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

