liveBook · Manning

HTTP 404 error

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

The HTTP 404 status code indicates that the server cannot find the resource or endpoint identified by the request path.

A 404 Not Found response means the requested endpoint or entity could not be found. ASP.NET Core generates one when no endpoint matches a URL; an endpoint can also return one when it matches the URL but cannot find the requested item. Whether to use 404 for a missing item is a matter of debate, so choose a convention that suits your API. section 15.5 section 5.3.1

A fruit ID that does not exist

For GET /fruit/{id}, a minimal API can return TypedResults.Ok(fruit) when the ID exists and Results.NotFound() when it does not. A web API controller can make the same choice by returning the fruit or calling NotFound(). In both cases, the route matches; the requested item is missing. section 5.3.1 section 20.1

Check the response as well as the request. Swagger UI lets you enter an ID, execute the GET request, and inspect the result. If it returns 404 but the OpenAPI description lists only 200, add response metadata so the documented possibilities match the API. section 11.2

Choose how much content a 404 response needs:

  • For a minimal API or web API, a status code without an additional response body is generally acceptable. section 15.5
  • For a Razor Pages app used directly by people, provide an error page rather than leaving them with a generic error display. Place StatusCodePagesMiddleware early: it handles bodyless 4xx and 5xx responses produced later in the pipeline. section 15.5
  • Use re-execution to generate a page through the application pipeline. A redirect instead sends a 302 response and makes the browser request the error-page URL separately. section 15.5
Figure 15.6 StatusCodePagesMiddleware reexecuting the pipeline to generate an HTML body for a 404 response. A request to the / path returns a 404 response, which is handled by the status code middleware. The pipeline is reexecuted using the /404 path to generate the HTML response.
CH15_F06_Lock3

The diagram traces a 404 through pipeline re-execution at /404, making the difference between detecting the error and generating its HTML page easier to see. section 15.5

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

How deep it goes
Explains when 404 occurs and compares API results with status-code-page handling; leaves the missing-item convention open to debate.
Examples
Uses a fruit lookup with a missing ID, a Swagger UI request exposing an undocumented 404, and a re-executed /404 page.
What you can do after
Apply it via Results.NotFound() or NotFound() for missing items, and configure StatusCodePagesMiddleware for user-facing error pages.
How it connects
Connects 404 to endpoint routing, minimal APIs, Razor Pages, StatusCodePagesMiddleware, OpenAPI, and Swagger UI.

Also explained in

Compare how 3 books explain HTTP 404 error →

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.