liveBook · Manning

ExceptionHandlerMiddleware

As explained in Building Web APIs with ASP.NET Core

ExceptionHandlingMiddleware is HTTP middleware that catches exceptions and forwards relevant error information to a configurable error handler.

ExceptionHandlerMiddleware catches HTTP-pipeline exceptions and sends relevant error information to a configurable handler instead of exposing a detailed error response. section 2.4.3

Use it outside development, where detailed exception pages could reveal stack traces, cookies, headers, configuration details, or vulnerabilities. Configure the middleware with an error route such as /error. section 2.4.3

A practical configuration follows these decisions:

  • Enable the developer exception page in development and ExceptionHandlerMiddleware in staging or production. section 2.4.3
  • Implement the configured /error handler with either an API controller or a Minimal API endpoint. section 2.4.3
  • Return a standardized problem response, such as HTTP 500 with a human-readable error title. section 2.4.3
  • Customize handling when needed by mapping exception types to status codes, logging exceptions, or notifying administrators. section 6.4

Route-based error handling

Configure app.UseExceptionHandler("/error") and implement either a controller action returning Problem() or app.MapGet("/error", () => Results.Problem()). Test with a route that throws an exception, then verify the resulting problem response. section 2.4.3

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/error");
}

Shows the environment-based Program.cs branch that selects the developer exception page or ExceptionHandlerMiddleware. section 2.4.3

using Microsoft.AspNetCore.Mvc;
 
namespace MyBGList.Controllers
{
    [ApiController]
    public class ErrorController : ControllerBase
    {
        [Route("/error")]             #1
        [HttpGet]                     #2
        public IActionResult Error()
        {
            return Problem();         #3
        }
    }
}

Shows a controller-based /error endpoint that converts failures into a Problem response. section 2.4.3

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains environment selection, route implementation, response generation, testing, and customization, but not middleware internals.
Examples
Uses /error controller and Minimal API endpoints, deliberate test exceptions, and environment or configuration switches.
What it assumes
Assumes basic ASP.NET Core pipeline and endpoint knowledge while teaching the handler route from controller and Minimal API options.
What you can do after
Operate/configure ASP.NET Core exception handling with a safe error route and customize status codes, logging, or notifications.
How it connects
Connects HTTP middleware with the Developer Exception Page, controllers, Minimal APIs, Problem Details, logging, and data validation.

Also explained in

Compare how 2 books explain ExceptionHandlerMiddleware →

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.