Action Result Overview in MVC

Action Result Overview in MVC

ASP.NET MVC has different types of Action Results. Each action result returns a different format of output. As a programmer, we need to use different action results to get the expected output. In this article, I will give you Action Result Overview in ASP.NET MVC.

Action Result Overview in MVC

What is the Action Method in ASP.NET MVC?

All the public methods inside a Controller which response to the URL is known as Action Method. When creating an Action Method we must follow these rules.

  1. Action method must be public.
  2. It cannot be overloaded
  3. It cannot be a static method
  4. ActionResult is a base class of all the result type action methods.
What is the Action Result in MVC?

Action Result is a result of action methods or return types of action methods. Action result is an abstract class. It is the base class for all type of action results. 

Action Result Overview in MVC

The above diagram describes the abstract class of Action Result. There are two methods in Action Result. One is ActionResult() and another one is ExecuteResult() as shown in the below diagram.

Action Result Overview in MVC

Why is ActionResult an abstract class?

It’s because different controller actions can return different types of results and still MVC handle them properly.

Types of Action Results

There are different Types of action results in ASP.NET MVC. Each result has a different type of result format to view page.

ActionResult is the base class of all the result type action method. Following are the Result types that an action method can return in MVC.

  1. ViewResult – Represents HTML and markup.
  2. EmptyResult – Represents no result.
  3. RedirectResult – Represents a redirection to a new URL.
  4. JsonResult – Represents a JavaScript Object Notation result that can be used in an AJAX application.
  5. JavaScriptResult – Represents a JavaScript script.
  6. ContentResult – Represents a text result.
  7. FileContentResult – Represents a downloadable file (with the binary content).
  8. FilePathResult – Represents a downloadable file (with a path).
  9. FileStreamResult – Represents a downloadable file (with a file stream).

Many of the derived classes we’re going to discuss have associated helpers. These helpers provide shortcuts to the constructor methods of their related Results. It’s what allows us to return View() rather than return new ViewResult()

Example:
public ActionResult ChooseView()
{
    if (DateTime.Now.Day % 2 == 0)
    {
        return View("View1");
    }
    else
    {
        return RedirectToAction("View2");
    }
}

You may be guessed that this 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.

Let’s have a look at these three categorized:

Content-returning:

These ActionResults are responsible for returning content to the browser or calling script. The examples are as follows

  1. ViewResult
  2. PartialViewResult
  3. FileResult
  4. ContentResult
  5. EmptyResult
  6. JsonResult
  7. JavaScriptResult
Redirection:

These ActionResults are responsible for redirecting to other URLs or actions. The examples are as follows

  1. RedirectResult
  2. RedirectToRouteResult
  3. RedirectToActionResult
Status:

These ActionResults are responsible for returning status codes to the browser for it to use. 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’s a good practice to return specific sub-types, but, if different paths of the action method return different subtypes, then I would return an ActionResult object. An example is shown below.

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

In the next article, I will discuss View Result and Partial View Result in ASP.NET MVC application.

SUMMARY

In this article, I try to explain Action Result Overview in ASP.NET MVC application. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article. 

Leave a Reply

Your email address will not be published. Required fields are marked *