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.

StatusCodePages middleware returns both JSON Problem Details & plaintext content for responses to requests from browsers

See original GitHub issue

Is 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

  1. Create a new ASP.NET Core Web API application (minimal)
  2. In Program.cs add a call to builder.Services.AddProblemDetails() to enable the IProblemDetailsService
  3. In Program.cs add a call to app.UseStatusCodePages() to add the StatusCodePagesMiddleware to the pipeline
  4. Run the application
  5. 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: image

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:closed
  • Created 9 months ago
  • Comments:20 (18 by maintainers)

github_iconTop GitHub Comments

2reactions
Tratchercommented, Jan 4, 2023

Oh, I thought you were referring to a fix for this issue. This issue should still be fixed regardless.

1reaction
dstarostacommented, Mar 18, 2023

Moving UseStatusCodePages() and UseExceptionHandler() 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.

Read more comments on GitHub >

github_iconTop 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 >

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