IActionResult represents instructions for producing an HTTP response. A Razor Page handler chooses and returns a result; the Razor Pages infrastructure executes it to generate the response. This separates the decision about what to send from the work of generating it, so each can be tested separately. section 15.4
Common result types include:
- PageResult generates HTML for the associated Razor Page with a 200 response; ViewResult generates HTML from a Razor view in an MVC controller. section 15.4
- RedirectToPageResult sends a 302 redirect to another page; RedirectResult sends a 302 redirect to a specified URL. section 15.4
- FileResult and its derived types return files; ContentResult returns a string; StatusCodeResult sends a raw HTTP status code. section 15.4
A Razor Page handler can also return void to render its associated template. Use an IActionResult when the handler needs different instructions, such as a redirect or an error response. In web APIs, controller actions can return raw .NET objects or results such as StatusCodeResult. section 13.1.4 section 20.1
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
}
This action returns a fruit value for a valid ID and NotFound() for an invalid ID. Its ActionResult<string> return type accommodates both outcomes. section 20.1
Choose IActionResult rather than IResult for Razor Pages and MVC controllers. IResult is used in minimal APIs; although IResult types execute in Razor Pages and MVC controllers in .NET 7, they do not have all the same features as IActionResult. section 15.4

