Loading

ASP.NET MVC

What is Action Results in ASP.NET MVC?. The Complete ASP.NET MVC Developer Course 2023 [Videos].

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.

Action Results in ASP.NET MVC

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.

  1. The action method must be public.
  2. It cannot be overloaded
  3. It cannot be a static method
  4. 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.

Action Results in MVC

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.

ASP.NET MVC Action Results

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.

  1. ViewResult â€“ Represents HTML and markup.
  2. PartialViewResult – Represents HTML and markup.
  3. EmptyResult â€“ Represents no result.
  4. RedirectResult â€“ Represents a redirection to a new URL.
  5. RedirectToActionResult â€“ It is returning the result to a specified controller and action method
  6. JsonResult â€“ Represents a JavaScript Object Notation result that can be used in an AJAX application.
  7. JavaScriptResult â€“ Represents a JavaScript script.
  8. ContentResult â€“ Represents a text result.
  9. FileContentResult â€“ Represents a downloadable file (with the binary content).
  10. FilePathResult â€“ Represents a downloadable file (with a path).
  11. 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().

<span style="color: #000000; font-family: arial, helvetica, sans-serif;">public ActionResult ChooseView()
{
if (DateTime.Now.Day % 2 == 0)
{
return View("View1");
}
else
{
return RedirectToAction("View2");
}
}
</span>
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: 

  1. Content-returning
  2. Redirection
  3. 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:

  1. ViewResult
  2. PartialViewResult
  3. FileResult
  4. ContentResult
  5. EmptyResult
  6. JsonResult
  7. 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:

  1. RedirectResult
  2. RedirectToRouteResult
  3. 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:

  1. HttpStatusCodeResult
  2. HttpUnauthorizedResult
  3. 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.

public ActionResult Index()
{
if (Your_Condition)
return View(); // returns ViewResult object
else
return Json("Data"); // returns JsonResult object
}

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.

public JsonResult Index()
{
return Json("Data"); // returns JsonResult object
}

See All

Comments (305 Comments)

Submit Your Comment

See All Posts

Related Posts

ASP.NET MVC / Youtube

What is MVC?

MVC is an architectural software design pattern that is used for developing interactive applications where their user interaction is involved and based on the user interaction some event handling has occurred. It is not only used for web-based applications but it can also be used for Desktop or mobile-based applications where there are user interactions involved.
28-jan-2022 /28 /305

ASP.NET MVC / Youtube

How to Creat First ASP.NET MVC Application using Visual Studio?

In this article, I am going to discuss how to create the first ASP.NET MVC Application step by step from scratch using Visual Studio 2015. You can use any version as per your choice but the step will remain the same. Please read our previous article before proceeding to this article where we gave a brief introduction to ASP.NET MVC Framework.
28-jan-2022 /28 /305

ASP.NET MVC / Youtube

What is ASP.NET MVC File and Folder Structure?

In this article, I am going to discuss the auto-generated ASP.NET MVC File and File Structure when we create a new ASP.NET MVC application. Please read our previous article before proceeding to this article where we discussed how to create ASP.NET MVC 5 application step by step from scratch.
28-jan-2022 /28 /305