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

