liveBook · Manning

NotFoundResult

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

NotFoundResult is an ASP.NET Core MVC action result representing an HTTP 404 Not Found response.

NotFoundResult represents an HTTP 404 Not Found response when the requested resource does not exist. It sends a raw status code through the middleware pipeline without generating HTML. section 15.4.2

Returning 404 from an API action

A controller can return the requested item when an ID is valid and return NotFound() otherwise: `public ActionResult<string> View(int id) { if (id >= 0 && id < _fruit.Count) return _fruit[id]; return NotFound(); }` With [ApiController], the 404 response is converted into the standard ProblemDetails format. section 20.1

Figure 15.4 If you return a raw 404 status code without any HTML, the browser will render a generic default page instead. The message is of limited utility to users and may leave many of them confused or thinking that your web application is broken.
CH15_F04_Lock3

A raw 404 contains no HTML, so a browser may display a generic default page with little useful guidance. section 15.4.2

Raw status codes suit APIs, but Razor Pages applications usually need an intercepted 404 response with a user-friendly HTML page. section 15.4.2

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

How deep it goes
Explains raw status-code behavior, API formatting, and browser consequences, but defers custom 404-page implementation.
Examples
Uses a missing e-commerce product, an invalid fruit identifier, a raw browser error page, and a ProblemDetails response.
What it assumes
Assumes basic Razor Pages or controller actions, while demonstrating IActionResult and ApiController behavior through examples.
What you can do after
Apply it via NotFound() to return a 404 from an API action and recognize when Razor Pages need custom HTML.
How it connects
Connects NotFoundResult with HTTP 404, StatusCodeResult, IActionResult, middleware, ProblemDetails, and the ApiController attribute.

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.