liveBook · Manning

Results.NotFound

As explained in .NET in Action, Second Edition

Results.NotFound is an ASP.NET Core factory method that creates an HTTP 404 Not Found result.

Results.NotFound creates an HTTP 404 response when a request cannot produce the requested result, such as when an ASCII-art font is invalid. section 3.1.2

A route can select Results.NotFound after a Boolean-returning method reports failure. Results.Content and Results.NotFound both implement IResult, so a ternary expression can choose between them. section 3.1.2

Invalid-font response

The route writes ASCII art when AsciiArt.Write succeeds; otherwise, it returns Results.NotFound(). section 3.1.2

Listing 3.16 Returning 404 if font is invalid
app.MapGet("/{text}",
  (string text, string? font) =>
    AsciiArt.Write(text, out var asciiText, font)    #1
      ? Results.Content(asciiText!)                  #2
      : Results.NotFound());                         #3

Inspect the focused minimal-route listing that uses an out parameter, Results.Content, and Results.NotFound in one response decision. section 3.1.2

How .NET in Action, Second Edition covers it

How deep it goes
Explains response selection through a focused route example, but does not detail result internals.
Examples
Uses an ASCII-art route that returns 404 when the requested font cannot be found.
What it assumes
Assumes familiarity with C# methods, Boolean results, out parameters, and ASP.NET Core route handlers.
What you can do after
Apply it via an ASP.NET Core route to report a missing or invalid requested resource.
How it connects
Connects HTTP 404 responses with Results.Content, IResult, out parameters, and ternary operators.

Related concepts

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