liveBook · Manning

IExceptionHandlerPathFeature

As explained in Building Web APIs with ASP.NET Core

IExceptionHandlerPathFeature is an ASP.NET Core feature interface that exposes the exception and request path associated with exception-handler re-execution.

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

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Shows retrieval and Error.Message usage, while deferring broader logging and notification implementations.
Examples
Uses an /error endpoint that retrieves Error.Message and returns a customized ProblemDetails payload.
What it assumes
Assumes familiarity with HttpContext, Minimal API handlers, exception-handling middleware, and ProblemDetails.
What you can do after
Build it from scratch: configure an error handler that reads exception details and returns a structured response.
How it connects
Connects exception-handling middleware with HttpContext, Minimal APIs, ProblemDetails, and trace identifiers.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.