ProblemDetails is not showing the trace id when AddProblemDetails is set before AddControllers #57935
Open
1 task done
Labels
area-mvc
Includes: MVC, Actions and Controllers, Localization, CORS, most templates
feature-problem-details
help wanted
Up for grabs. We would accept a PR to help resolve this issue
Is there an existing issue for this?
Describe the bug
A .NET 8.0 application with ProblemDetails enabled not showing the traceId when AddProblemDetails is called before AddControllers. This happens in both, when using the WebApplicationBuilder or using the CreateDefaultWebhost with an startup class. This only happens when the Accept header sent by the client is application/json.
Expected Behavior
When an exception occurs in one controller, the problem details response shows the traceId or the documentation clearly warns you to call AddProblemDetails after the AddControllers call.
Order is important in middleware but not when configuring services.
Steps To Reproduce
Create a new .net 8.0 application with the following Program.cs
Create a controller that throws an exception
Call the endpoint, the traceId is shown regardless of the Accept header sent
Swap the order of the AddControllers and AddProblemDetails calls in the Program.cs
Call the endpoint and the traceId is not shown when the Accept header is "application/json"
Any other Accept header will produce the traceId
Exceptions (if any)
No response
.NET Version
8.0.400
Anything else?
Tested in Visual Studio 2022 or running dotnet run from the console.
Dotnet info output
The text was updated successfully, but these errors were encountered: