liveBook · Manning

IActionResult

As explained in Pro ASP.NET Core 7, Tenth Edition

IActionResult is the ASP.NET Core MVC interface implemented by objects that represent the result of processing an HTTP request.

IActionResult is the ASP.NET Core MVC interface for an action result: an object that tells MVC what response to send after a controller action handles a request. An action can choose a response without writing directly to HttpResponse. The same interface serves web-service controllers and controllers that render HTML. section 19.5.3 section 21.2.2

Choose a result through a controller method:

  • In a web-service controller, ControllerBase provides Ok for a 200 response with optional data, NoContent for 204, NotFound for 404, and BadRequest for 400. Returning an object directly is equivalent to Ok(object); returning null is equivalent to NoContent(). section 19.5.3
  • In an HTML controller, Controller adds View. It returns a ViewResult, which implements IActionResult and asks MVC to use a view to produce the response. section 21.2.2

Return a product or a 404

In ProductsController, a GET action can return Task<IActionResult> and select its result after the lookup: [HttpGet("{id}")] public async Task<IActionResult> GetProduct(long id) { Product? p = await context.Products.FindAsync(id); if (p == null) { return NotFound(); } return Ok(p); } A request for /api/products/1000 that finds no product now receives 404 rather than the earlier 204 response. PowerShell’s Invoke-WebRequest reports the error status as an exception. section 19.5.3

Return a view

An HTML controller can return View(await context.Products.FindAsync(id)) from an Index action declared as Task<IActionResult>. The product becomes the view model. If no view is available, requesting http://localhost:5000 shows an error rather than a rendered page. section 21.2.2

Table 19.7 Useful ControllerBase action result methods (view table figure)

Name

Description

Ok

The IActionResult returned by this method produces a 200 OK status code and sends an optional data object in the response body.

NoContent

The IActionResult returned by this method produces a 204 NO CONTENT status code.

BadRequest

The IActionResult returned by this method produces a 400 BAD REQUEST status code. The method accepts an optional model state object that describes the problem to the client, as demonstrated in the "Validating Data" section.

File

The IActionResult returned by this method produces a 200 OK response, sets the Content-Type header to the specified type, and sends the specified file to the client.

NotFound

The IActionResult returned by this method produces a 404 NOT FOUND status code.

Redirect

RedirectPermanent

The IActionResult returned by these methods redirects the client to a specified URL.

RedirectToRoute

RedirectToRoutePermanent

The IActionResult returned by these methods redirects the client to the specified URL that is created using the routing system, using convention routing, as described in the "Redirecting Using Route Values" sidebar.

LocalRedirect

LocalRedirectPermanent

The IActionResult returned by these methods redirects the client to the specified URL that is local to the application.

RedirectToAction

RedirectToActionPermanent

The IActionResult returned by these methods redirects the client to an action method. The URL for the redirection is created using the URL routing system.

RedirectToPage

RedirectToPagePermanent

The IActionResult returned by these methods redirects the client to a Razor Page, described in chapter 23.

StatusCode

The IActionResult returned by this method produces a response with a specific status code.

Compare the ControllerBase methods available for status-code, file, and redirect responses. section 19.5.3

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Explains result-producing controller methods and demonstrates their request behavior; does not descend into IActionResult implementation internals.
Examples
Uses product GET and POST requests, PowerShell responses, redirects, and an HTML action returning a product view model.
What it assumes
Assumes controller actions and HTTP requests as context while introducing the methods that create action results.
What you can do after
Apply it via ASP.NET Core MVC to return status-code, redirect, or view results from controller actions.
How it connects
Connects controller actions to HTTP status codes, ControllerBase, ViewResult, views, and routing.

Also explained in

Compare how 2 books explain IActionResult →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.