liveBook · Manning

IActionResult

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

IActionResult is an ASP.NET Core interface representing instructions for producing an HTTP response from request-handling code.

IActionResult represents instructions for producing an HTTP response in ASP.NET Core. A Razor Page handler decides what kind of response is needed and returns a result; the Razor Pages infrastructure executes that result to generate the response. This keeps the handler’s decision separate from response generation, so each can be tested independently. section 15.4

A page handler can return void when it should render its associated Razor template. Return an IActionResult when it needs different instructions, such as redirecting the user or sending an error. Razor Pages usually produce HTML, although a result can return other data. section 13.1.4

Choose a result for the response you need:

  • PageResult renders the associated Razor Page as HTML with a 200 response; ViewResult renders a Razor view for an MVC controller. section 15.4
  • RedirectToPageResult redirects to another page; RedirectResult redirects to a specified URL. Both send a 302 response. section 15.4
  • FileResult returns a file, ContentResult returns a string, and StatusCodeResult sends an HTTP status code. section 15.4

Web API controller actions can also return an IActionResult, particularly when they need to represent an error. They may instead return raw .NET objects, which are serialized into the response body. For Razor Pages and MVC controllers, favor IActionResult over the minimal-API interface IResult: even though IResult executes there in .NET 7, it does not provide all the same features. section 20.1 section 15.4

Listing 20.3 A web API action returning IActionResult to handle error conditions
[HttpGet("fruit/{id}")]                     #1
public ActionResult<string> View(int id)    #2
{
    if (id >= 0 && id < _fruit.Count)       #3
    {
        return _fruit[id];                  #4
    }
    return NotFound();                      #5
}

The controller action returns ActionResult<string>: a valid index returns a string, while an invalid index returns NotFound(). This shows why an action may need a result alongside its usual data response. section 20.1

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

How deep it goes
Explains when results execute, why they separate decisions from generation, and what common result types return; stops short of implementing a custom result.
Examples
Uses HTML, redirect, file, and status-code results, plus an indexed web API action that returns data or NotFound().
What it assumes
Builds on HTTP requests and handler methods while explaining result execution rather than assuming familiarity with IActionResult.
What you can do after
Apply it via ASP.NET Core Razor Page handlers or controller actions to choose an appropriate response and test that choice separately.
How it connects
Connects Razor Page handlers, MVC controllers, web API actions, HTTP status codes, redirects, and the distinct minimal-API IResult interface.

Also explained in

Compare how 2 books explain IActionResult →

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.