concept IActionResult in category asp.net

appears as: IActionResult, IActionResult
ASP.NET Core in Action

This is an excerpt from Manning's book ASP.NET Core in Action.

Once the middleware has selected a controller and action for a given request, the appropriate class will be instantiated and the action method invoked. Controllers are ordinary classes, though they often inherit from the Controller base class to provide access to a number of helper methods, as shown in the following listing. Similarly, there’s nothing special about action methods, other than that they typically return an IActionResult, which contains instructions for handling the request.

As a parallel to this, you create Web API controllers in ASP.NET Core in the very same way you create traditional MVC controllers. The only thing that differentiates them from a code perspective is the type of data they return—MVC controllers typically return a ViewResult; Web API controllers generally return raw .NET objects from the action methods, or an IActionResult such as StatusCodeResult, as you saw in chapter 4.

Listing 9.2 shows an example of a case where you’d want to return an IActionResult instead of returning the data directly. It shows another method on the same FruitController as before. This method exposes a way for clients to fetch a specific fruit by an id, which we’ll assume is its index in the list of _fruit you defined in the previous listing. Model binding is used to set the value of the id parameter from the request.

Listing 9.2. A Web API action returning IActionResult to handle error conditions
public IActionResult View(int id) #1 { if (id >= 0 && id < _fruit.Count) #2 { return Ok(_fruit[id]); #3 } return NotFound(); #4 } #1 The action method returns an IActionResult as it can no longer always return List<string>. #2 An element can only be returned if the id value is a valid _fruit element index. #3 Using Ok to return data will format the data passed to it and send a 200 status code. #4 NotFound returns a NotFoundResult, which will send a 404 status code. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":44},{\"line\":0,\"ch\":46}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":2}],[{\"line\":2,\"ch\":44},{\"line\":2,\"ch\":46}],[{\"line\":11,\"ch\":0},{\"line\":11,\"ch\":2}],[{\"line\":4,\"ch\":44},{\"line\":4,\"ch\":46}],[{\"line\":12,\"ch\":0},{\"line\":12,\"ch\":2}],[{\"line\":7,\"ch\":44},{\"line\":7,\"ch\":46}],[{\"line\":13,\"ch\":0},{\"line\":13,\"ch\":2}]]"} !@%STYLE%@!

1Some people get uneasy when they see the phrase “helper method,” but there’s nothing magic about the Controller helpers—they’re shorthand for creating a new IActionResult of a given type. You don’t have to take my word for it though, you can always view the source code for the base class on GitHub at http://mng.bz/NZDt.

Figure 9.4. Returning data using the Ok helper method. The Ok method uses the data passed to it to create the response body, then sets the status code to 200.

If the id is outside the bounds of the _fruit list, then the method calls NotFound to create a NotFoundResult. When executed, this method generates a raw 404 HTTP status code response, which will show the default “not found” page for your browser, as shown in figure 9.5. The fact that you’re returning two different types depending on the code path taken means that you must mark the action method as returning an IActionResult in order to make the code compile.

Figure 9.5. A request that generates a raw 404 response without any content, as is typical for a Web API, will show the default “not found” page in a browser.

You’re free to return any type of IActionResult from your Web API controllers, but you’ll commonly return StatusCodeResult instances, which return a specific status code with or without associated data. NotFoundResult and OkResult both derive from StatusCodeResult, for example. Another commonly used response is a 400 (bad request), which is normally returned when the data provided in the request fails validation. This can be generated using a BadRequestResult, often using the BadRequest helper method.

  • Web API action methods can return data directly or can use IActionResult to generate an arbitrary response.