In this article, I am going to give an overview of the Action Results in the ASP.NET MVC Application. In ASP.NET MVC Application, there are many different types of Action Results. Each action result returns a different format of the output. As a programmer, we need to use different action results to get the expected output. So, at the end of this article, you will understand the different types of Action Results and when to use which action results in MVC Application.
What is the Action Method in ASP.NET MVC?
Before going to understand Action Results, first, we need to understand what are action methods in ASP.NET MVC Application. All the public methods inside a Controller which respond to the URL are known as Action Methods. When creating an Action Method, we must follow the below rules.
- The action method must be public.
- It cannot be overloaded
- It cannot be a static method
- ActionResult is the base class of all the result types that an action method returns.
What is the Action Result in ASP.NET MVC?
Action Result is the return type of an action method. The action result is an abstract class. It is the base class for all types that an action method returns. As you can see in the below image, View, Partial View, Redirect, Json, Content, File, Redirect To Action, etc. are derived from the abstract Action Result class and these types can also be used as the return type of an action method.
ActionResult Class:
The following diagram shows the signature of the ActionResult class. As you can see in the below image, ActionResult is an Abstract class having one constructor and one method. The constructor is basically used to initializes a new instance of the ActionResult class. The ExecuteResult method enables the processing of the result of an action method by a custom type that inherits from the ActionResult class. The ExecuteResult method takes one parameter i.e. context i.e. the context in which the result is executed. The context includes the information of Controller, HTTP Content, request context, and route data.
Why is ActionResult an abstract class in ASP.NET MVC?
Its because different controller action methods can return different types of results as per the business needs and still the ASP.NET MVC Framework handles them properly. If you mention the return type of an action method as ActionResult, then this action method can return any type which is derived from the ActionResult abstract class.
Types of Action Results
There are many different types of Action Results that an action method can return in ASP.NET MVC. Each Action Result returns a different type of result format. ActionResult is the base class of all the result types. The following are the Result types that an action method can return in ASP.NET MVC Application.
- ViewResult – Represents HTML and markup.
- PartialViewResult – Represents HTML and markup.
- EmptyResult – Represents no result.
- RedirectResult – Represents a redirection to a new URL.
- RedirectToActionResult – It is returning the result to a specified controller and action method
- JsonResult – Represents a JavaScript Object Notation result that can be used in an AJAX application.
- JavaScriptResult – Represents a JavaScript script.
- ContentResult – Represents a text result.
- FileContentResult – Represents a downloadable file (with the binary content).
- FilePathResult – Represents a downloadable file (with a path).
- FileStreamResult – Represents a downloadable file (with a file stream).
Many of the derived classes were going to discuss have associated helpers. These helpers provide shortcuts to the constructor methods of their related Results. That allows us to write return View() rather than return new ViewResult().
Example:
In the below example, the action method return type is ActionResult, and the method returning two types of results. First is, return View which is similar to return new ViewResult(). Here, View() is the shortcut for new ViewResult(). Second is, return RedirectToAction(); which is similar to return new RedirectToActionResult(). Here, RedirectToAction() is the shortcut for new RedirectToActionResult().
Categorized of Action Results:
You may be guessed that the above example implementation is done because ActionResult has a lot of derived classes, and you are absolutely right. But what exactly are these different kinds of results? These results are categorized into three sections:
- Content-returning
- Redirection
- Status.
Lets have a look at these three categorized:
Content-Returning Action Result in ASP.NET MVC:
The Content-Returning ActionResults in ASP.NET MVC are responsible for returning content to the browser or calling the script. The examples are as follows:
- ViewResult
- PartialViewResult
- FileResult
- ContentResult
- EmptyResult
- JsonResult
- JavaScriptResult
Redirection Action Result in ASP.NET MVC:
The Redirection ActionResults in ASP.NET MVC are responsible for redirecting to other URLs or actions. The examples are as follows:
- RedirectResult
- RedirectToRouteResult
- RedirectToActionResult
Status Action Result in ASP.NET MVC:
The Status ActionResults in ASP.NET MVC are responsible for returning status codes to the browser. The examples are as follows:
- HttpStatusCodeResult
- HttpUnauthorizedResult
- HttpNotFoundResult
What should be the return type of an action method – ActionResult or specific derived type?
It basically depends on the situation. If your action method returns one type of result, then it is good to use a specific derived type based on the return value. But, if your action method returns different kinds of results based on different conditions, then you should use ActionResult as the return type. For better understanding, please have a look at the below example. As, the Index method returning two types of Results i.e. View Result and Json Result, so, we are using the return type of the Action method as ActionResult.
In the below example, the action method going to return one type of result i.e. JsonResult, so, it is advisable to use JsonResult as the return type of the Action method.