IExceptionHandlerPathFeature is an ASP.NET Core feature interface that lets an exception handler retrieve the originating exception while handling a re-executed request. section 6.2.4
Use it inside an exception-handling endpoint or middleware to:
- Retrieve the feature through context.Features.Get<IExceptionHandlerPathFeature>(). section 6.2.4
- Read Error.Message and place it in a ProblemDetails response when additional diagnostic detail is appropriate. section 6.2.4
- Support follow-up actions such as exception logging, notifications, or exception-specific status codes. 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);
});
A Minimal API error endpoint retrieves the feature, copies the exception message into ProblemDetails, and adds a trace identifier. section 6.2.4
