UseStatusCodePagesWithReExecute with UseStaticFiles results in wrong status code (200 OK)
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Describe the bug
I have a few static files, one for each status code. I want to display those when an error code happens. I’m using a combination of UseStatusCodePagesWithReExecute
and UseStaticFiles
. However the StaticFilesMiddleware
will replace the existing status code with 200, so now any error page is served with 200 OK instead of the actual status code.
app.UseStatusCodePagesWithReExecute("/Errors/{0}.html");
app.UseStaticFiles();
Expected Behavior
The status code should reflect the actual error.
Steps To Reproduce
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.UseStatusCodePagesWithReExecute("/error.html");
app.UseStaticFiles();
app.MapGet("/", async context => { context.Response.StatusCode = 500; });
app.Run();
Exceptions (if any)
No response
.NET Version
6.0.200
Anything else?
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (12 by maintainers)
Top Results From Across the Web
When using UseStatusCodePagesWithReExecute ...
When I enter an invalid url, /error/404.html is displayed as expected, but the browser gets a 200 status code, instead of the expected...
Read more >Retrieving the path that generated an error with ...
In this post, I show how you can obtain the original path that triggered an error status code when using the StatusCodePagesMiddleware's ...
Read more >Fixing ASP.NET Core's UseStatusCodePages Middleware
Using UseStatusCodePagesWithReExecute , The middleware always returned a 200 OK response, even if we were seeing our error page.
Read more >Global Error Handling in ASP.NET Core MVC
The caller will see a 200 status code from the loading of the error page but not the actual status code which triggered...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, we should do both. Make individual components behave better, but store the status code in the feature for when something doesn’t. I’ll have to find the MVC example, it’s somewhere in ActionResults.
Reopening based on feedback from https://github.com/dotnet/AspNetCore.Docs/issues/26935.
We could improve this by saving the status code on IStatusCodeReExecuteFeature so that it is available in case the response generator overwrites it, like in StaticFile’s OnPrepareResponse. We could also avoid setting default response status codes (200) in static files and mvc.