question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

UseStatusCodePagesWithReExecute with UseStaticFiles results in wrong status code (200 OK)

See original GitHub issue

Is 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:closed
  • Created 2 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
Tratchercommented, Feb 8, 2023

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.

1reaction
Tratchercommented, Jan 27, 2023

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.

Read more comments on GitHub >

github_iconTop 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 >
Handle errors in ASP.NET Core
Discover how to handle errors in ASP.NET Core apps.
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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found