Description
Is there an existing issue for this?
- I have searched the existing issues
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
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddProblemDetails();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseExceptionHandler();
app.UseStatusCodePages();
app.UseHttpsRedirection();
app.MapControllers();
app.Run();
Create a controller that throws an exception
[ApiController]
[Route("[controller]")]
public class PDController : ControllerBase
{
[HttpGet]
[Route("exception")]
public IActionResult GetSystemException()
{
throw new Exception("This is a test 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
builder.Services.AddProblemDetails();
builder.Services.AddControllers();
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
.NET SDK:
Version: 8.0.400
Commit: 36fe6dda56
Workload version: 8.0.400-manifests.6c274a57
MSBuild version: 17.11.3+0c8610977
Runtime Environment:
OS Name: Windows
OS Version: 10.0.22000
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\8.0.400\
.NET workloads installed:
Configured to use loose manifests when installing new manifests.
[aspire]
Installation Source: VS 17.11.35303.130
Manifest Version: 8.1.0/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.aspire\8.1.0\WorkloadManifest.json
Install Type: FileBased
Host:
Version: 8.0.8
Architecture: x64
Commit: 08338fcaa5