liveBook · Manning

IResult

As explained in .NET in Action, Second Edition

IResult is an ASP.NET Core interface representing the result of executing an HTTP endpoint.

IResult represents the result produced by executing an ASP.NET Core HTTP endpoint. Endpoint handlers can return different responses through the same interface, such as successful content or a 404 response, without using exceptions for expected failure. section 3.1.2

A practical endpoint can use IResult to:

  • Return generated text with Results.Content when an operation succeeds. section 3.1.2
  • Return Results.NotFound when AsciiArt.Write reports that the requested font is invalid. section 3.1.2
  • Return image bytes with Results.File and an image/jpeg content type. 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

This focused endpoint uses a ternary operator because Results.Content and Results.NotFound both produce IResult values. section 3.1.2

Content or 404

AsciiArt.Write(text, out var asciiText, font) ? Results.Content(asciiText!) : Results.NotFound() section 3.1.2

How .NET in Action, Second Edition covers it

How deep it goes
Defines the shared return type through focused endpoint examples, but does not detail IResult internals.
Examples
Uses ASCII-art text, invalid fonts, barcode images, Results.Content, Results.NotFound, and Results.File.
What it assumes
Assumes familiarity with C# methods, Boolean results, out parameters, and ASP.NET Core endpoint mapping.
What you can do after
Apply it via ASP.NET Core endpoint handlers to select content, not-found, or file responses from operation results.
How it connects
Connects IResult with ASP.NET Core endpoint handlers, ternary operators, HTTP status responses, content responses, and file responses.

Also explained in

Compare how 2 books explain IResult →

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.