StatusCodePages middleware returns both JSON Problem Details & plaintext content for responses to requests from browsers
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Describe the bug
The StatusCodePagesMiddleware
is returning both JSON Problem Details and plain text content (concatenated together) when the request’s Accept
header contains certain combinations of media types. This can be easily seen by making a request from a browser (reproduced using Edge latest on Windows).
Expected Behavior
The response should only contain either plain text or JSON Problem Details content, depending on the value of the Accept
header.
Steps To Reproduce
- Create a new ASP.NET Core Web API application (minimal)
- In
Program.cs
add a call tobuilder.Services.AddProblemDetails()
to enable theIProblemDetailsService
- In
Program.cs
add a call toapp.UseStatusCodePages()
to add theStatusCodePagesMiddleware
to the pipeline - Run the application
- Make a request from a browser to “/test” to force a 404 response
Example Program.cs
:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddProblemDetails();
var app = builder.Build();
app.UseStatusCodePages();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.MapGet("/", () => new { hello = "World" });
app.Run();
Result:
Accet
header content for request from browser:
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Exceptions (if any)
No response
.NET Version
7.0.200-preview.22605.5
Anything else?
No response
Issue Analytics
- State:
- Created 9 months ago
- Comments:20 (18 by maintainers)
Top Results From Across the Web
Handle errors in ASP.NET Core
DeveloperExceptionPageMiddleware: Generates a problem details response in development when the Accept request HTTP header does not include text/html .
Read more >Status Code Pages in Web API apps in ASP.NET Core
We want to return a 'Problem' response for all errors 400-599. The reason I asked for StatusCodePages is that there are certain status...
Read more >How to handle errors in minimal APIs in ASP.NET Core
The Developer Exception Page middleware displays detailed error information in the browser when an exception occurs. Here is an example of how ...
Read more >Handling errors in ASP.NET Core Web API - DevTrends
The main problem is that by using a string in the NotFound method, the framework will return this string as a plain text...
Read more >Problem Details responses everywhere with ASP.NET ...
It was possible to return the Problem Details response manually, or the framework could generate it automatically in several specific cases.
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 Free
Top 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
Oh, I thought you were referring to a fix for this issue. This issue should still be fixed regardless.
Moving
UseStatusCodePages()
andUseExceptionHandler()
to the top was the first thing I have tried. But it did not work until I updated Visual Studio (I was on the latest minor but not the patch version) and the SDK. Now everything works correctly.Thanks. Both answers were very helpful.