liveBook · Manning

DeveloperExceptionPageMiddleware

As explained in ASP.NET Core in Action, 3e

A developer exception page is an ASP.NET Core diagnostic page that displays detailed information about an unhandled exception during development.

DeveloperExceptionPageMiddleware helps debug unhandled exceptions during ASP.NET Core development. When an exception propagates up the middleware pipeline to it, the middleware captures the exception and returns a diagnostic HTML page with a 500 status code. section 4.3.1

The page shows the exception stack trace, the location and source line where the exception occurred, and request details. Query, Cookies, Headers, and Routing controls reveal further information about the request. section 4.3.1

Figure 4.15 The developer exception page shows details about the exception when it occurs during the process of a request. The location in the code that caused the exception, the source code line itself, and the stack trace are all shown by default. You can also click the Query, Cookies, Headers, and Routing buttons to reveal further details about the request that caused the exception.
CH04_F15_Lock3

Inspect the diagnostic page and its request-detail controls. section 4.3.1

WebApplication automatically adds this middleware at the start of the pipeline in the Development environment. You can therefore omit an explicit call to app.UseDeveloperExceptionPage() in that setup. When adding it explicitly, place it early enough in the pipeline to receive exceptions from later middleware. section 4.2.3 section 4.3.1

Registering the middleware

Call app.UseDeveloperExceptionPage(); when explicitly adding the middleware to a development pipeline. With WebApplication in Development, the automatic addition normally makes this call unnecessary. section 4.3.1

Never use the developer exception page in production: its diagnostic details can expose application code and other sensitive information. Use ExceptionHandlerMiddleware instead to return a controlled, user-friendly error response. Applications commonly add it conditionally when the environment is not Development. section 4.3.1 section 4.3.2

Listing 4.5 Adding exception-handler middleware when in production
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
WebApplication app = builder.Build();                                #1
 
if (!app.Environment.IsDevelopment())                                #2
{
    app.UseExceptionHandler("/error");                               #3
}
  
// additional middleware configuration
app.MapGet("/error", () => "Sorry, an error occurred");              #4

See a conditional production handler with an /error endpoint. section 4.3.2

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains exception capture, diagnostic page contents, registration, and production risks without building custom middleware.
Examples
Shows a diagnostic page with stack trace and request details, plus a conditional production handler using an /error endpoint.
What you can do after
Diagnose/debug request exceptions using the developer exception page and select a safer handler for production.
How it connects
Connects middleware pipelines and hosting environments with exception handling and ExceptionHandlerMiddleware.

Also explained in

Compare how 2 books explain DeveloperExceptionPageMiddleware →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.