TOC

The community is working on translating this tutorial into Georgian, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".

Controllers:

Action Results

In the previous article, we discussed Actions, which are the methods defined on a Controller. Actions are invoked when a requested URL is matched to an Action on a Controller (this is done by the Routing system). When the Action (method) finishes it work, it will usually return something to the client and that something will usually be implementing the IActionResult interface (or Task<IActionResult> if the method is asynchronous).

As soon as your Controller inherits the built-in base Controller class (Microsoft.AspNetCore.Mvc.Controller), we are supplied with a wide range of helper methods for generating the response (the action result). An example of such a method has already been used in this tutorial: The View() method. It will take the matching View file and turn it into an instance of the ViewResult class, which implements the IActionResult interface. After that, ASP.NET MVC will automatically transform it into a response to the browser - in this case in the form of an "OK" status code and then a body of HTML.

A view is far from the only possible result of a Controller action, though. Since the result will eventually be returned to a browser, the available methods should cover all possible outcomes of a HTTP request, so a Controller action can of course also result in a redirect, a 404 (Page not Found) or any of the other HTTP status codes. Here's an incomplete list of the most interesting and useful methods for generating an Action result:

  • Content() - returns the specified string as plain text to the client (usually a browser)
  • View() - returns a View to the client
  • PartialView() - returns a Partial View (more on those elsewhere in this tutorial) to the client
  • File() - returns the content of a specified file to the client
  • Json() - returns a JSON response to the client
  • Redirect() and RedirectPermanent() - returns a redirect response to the browser (temporary or permanent), redirecting the user to another URL
  • StatusCode() - returns a custom status code to the client

There are many more, and you are of course free to implement your own or generate the result your self - most of these methods simply generates the relevant class with properties set accordingly. For instance, the Content() method simply generates an instance of the ContentResult class and fills the Content property with the value you pass into the method.

Since there are many types of responses which implements the IActionResult interface, your actions can return any of them, based on the logic of your method. A common use case for this is to return either a View or a piece of content if the requested content is found, or a 404 (Page not Found) error if its not found. It could look like this:

public IActionResult Details(int id)  
{  
	Product product = GetProduct(id);
	if (product != null)  
		return View(product);  
	return NotFound();  
}

In the first line, we try to load the requested product, using a to-be-implemented GetProduct() method. If the product is found, we return it inside of a View using the View() method - if not, we return a 404 error using the NotFound() helper method, which basically just creates an instance of the NotFoundResult class. So in this case, there are two possible outcomes of the action, but there could be even more, if needed - you are free to return anything that implements the IActionResult interface.

Summary

An Action is a method on a Controller, usually accessible by a URL thanks to the Routing system. An Action will usually return an Action Result, based on the IActionResult interface. There are many helper methods available to aid you in generating the desired result(s) for your Action. These are the most important aspects to understand about an ASP.NET MVC Controller. In the coming articles we'll dig a bit deeper into some of the more advanced Controller topics.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!