liveBook · Manning

Error-handling path

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

An error handling path is a URL path designated to generate a response for an error status code.

An error-handling path is a URL path that generates the response shown when an exception or error status code occurs. Middleware can reexecute the request pipeline through this path, allowing the error response to use the application's normal layout and dynamic content. section 4.3.2 section 15.5

Route exceptions to /error

if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/error"); } app.MapGet("/error", () => "Sorry, an error occurred"); When an endpoint throws, ExceptionHandlerMiddleware clears any partial response, changes the request path to /error, reruns the pipeline, and finally returns status code 500. section 4.3.2

Status-code middleware can use a format token such as /{0}. A 404 response then reexecutes the pipeline through /404, while other status codes use their own paths. The handler can tailor the response to the status code. section 15.5

Choose and protect the error-handling path carefully:

  • Use a dynamic path when error pages should share menus, layouts, user information, or other generated content. section 4.3.2
  • Prefer a static fallback when the dynamic error path might fail, because another error can produce a generic browser error. section 15.5
  • Keep production exception responses generic so sensitive application details are not exposed. section 4.3.2
Figure 4.18 ExceptionHandlerMiddleware handling an exception to generate a JSON response. A request to the / path generates an exception, which is handled by the middleware. The pipeline is reexecuted, using the /error path to generate the JSON response.
CH04_F18_Lock3

Trace an exception from the original request through pipeline reexecution at /error and into the final JSON response. section 4.3.2

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

How deep it goes
Explains path configuration, pipeline reexecution, formatted status-code paths, response rewriting, and failure risks.
Examples
Uses /error for exceptions, /{0} for status-code pages, /404 for missing resources, and static or dynamic error responses.
What you can do after
Configure exception and status-code middleware to generate consistent, status-aware error pages.
How it connects
Connects error-handling paths with middleware pipelines, ExceptionHandlerMiddleware, StatusCodePagesMiddleware, Razor Pages, and HTTP status codes.

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.