ProblemDetails represents machine-readable details for an HTTP API error. It produces an RFC 7807-compliant JSON response so clients receive a consistent status, title, type, and optional diagnostic information. section 6.2.4
An exception endpoint can return Results.Problem() directly, or construct a ProblemDetails instance to add the exception message and a trace identifier before returning the response. section 6.2.4
A customized error response can:
- Set the HTTP status explicitly, such as 500 for an unhandled exception. section 6.2.4
- Copy the exception message into detail for a more informative client response. section 6.2.4
- Add extensions such as traceId to connect a client error with server diagnostics. section 6.2.4
- Provide a place for logging, notifications, and exception-specific handling. section 6.2.4
Customized exception response
A request to /error/test can produce a JSON body containing type, title, status 500, detail "test", and a traceId. This replaces a minimal generic error with information useful for client handling and investigation. section 6.2.4
app.MapGet("/error",
[EnableCors("AnyOrigin")]
[ResponseCache(NoStore = true)] (HttpContext context) => #1
{
var exceptionHandler =
context.Features.Get<IExceptionHandlerPathFeature>(); #2
// TODO: logging, sending notifications, and more #3
var details = new ProblemDetails();
details.Detail = exceptionHandler?.Error.Message; #4
details.Extensions["traceId"] =
System.Diagnostics.Activity.Current?.Id
?? context.TraceIdentifier;
details.Type =
"https://tools.ietf.org/html/rfc7231#section-6.6.1";
details.Status = StatusCodes.Status500InternalServerError;
return Results.Problem(details);
});
Inspect the complete Minimal API handler that retrieves the exception, populates ProblemDetails, adds traceId, and returns Results.Problem(details). section 6.2.4

