Controller Action Result (1), In ASP.NET MVC

This series of articles is to discuss the Action Results for ASP.NET (Core) MVC and Web API.

This article discusses Controller Action Results for ASP.NET MVC.

Introduction

This article will include the following content:

  • A- Controller
  • B - Controller Actions
  • C - Action Results
  • D - All Derived Action Results
  • E - Return Action Results by a Method from Controller base class

A - Controllers

MVC controllers are responsible for responding to requests made against an ASP.NET MVC website. Each browser request is mapped to a particular controller. For example

http://localhost/Product/Index/3

In this case, a controller named ProductController is invoked. 

B - Controller Actions

A controller exposes controller actions. An action is a method on a controller that gets called when you enter a particular URL in your browser address bar. For example

http://localhost/Product/Index/3

In this case, the Index() method is called on the ProductController class. The Index() method is an example of a controller action.

All the public methods of the Controller class are called Action methods. They are like any other normal methods with the following restrictions:

  1. Action method must be public. It cannot be private or protected
  2. Action method cannot be overloaded
  3. Action method cannot be a static method.

The following illustrates the Index() action method in the StudentController class.

Action Method in ASP.NET MVC

Action Method

As you can see in the above figure, the Index() method is public, and it returns the ActionResult using the View() method. The View() method is defined in the Controller base class, which returns the appropriate ActionResult.

C - Action Results

An action result is what a controller action returns in response to a browser request.

MVC framework includes various Result classes, which can be returned from an action method. The result classes represent different types of responses, such as HTML, file, string, JSON, javascript, etc. The following list all the result classes available in ASP.NET MVC, ASP.NET Core MVC, ASP.NET Web API, and ASP.NET Core Web API.

The ASP.NET MVC framework supports several types of action results including:

  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).

or

Controller Action Result (1), in ASP.NET MVC

Action method in ASP.NET MVC

We can demonstrate it as

Controller Action Result (1), in ASP.NET MVC

Different Types Of Action Results In ASP.NET MVC

All of these action results inherit from the base ActionResult class, such as

Controller Action Result (1), in ASP.NET MVC

D - All ASP.NET MVC supported Action Results

We have already given the 9 (directly) derived classes from the base ActionResult class shown above graph that is from MS ActionResult Class. However, we found out we list several types of action results are not in the derived class of base ActionResult class, such as  

  • FilePathResult - Represents a downloadable file (with a path).
  • FileStreamResult - Represents a downloadable file (with a file stream).

In fact, from MS ActionResult Class, it only lists the first level derived classes, there are still some second level derived classes that are no shown.

We try to find out the secondary and all derived classes from ActionResult class. We can use the Microsoft tool ILSpy (if you feel a bit difficult, you may go to this article: Action Result In ASP.NET MVC to get some details for get this tool) to get them easily:

The first level derived class for ActionResult classes (they are from System.Web.Mvc Assembly):

Controller Action Result (1), in ASP.NET MVC

The secondary level ActionResult classes:

Controller Action Result (1), in ASP.NET MVC

Therefore, we have a total derived classes from ActionResult class:

  1. HttpStatusCodeResult
    • HttpNotFoundResult
    • HttpUnauthorizedResult
  2. ViewResultBase:
    • PartialViewResult
    • ViewResult - Represents HTML and markup.
  3. EmptyResult - Represents no result.
  4. RedirectResult - Represents a redirection to a new URL.
  5. RedirectRouteResult
  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. FileResult:
    • 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).

Adding the missing derived classes of base ActionResult class in green, then we got the 9 first level derived classes with 7 secondary level derived classes, totally 16 derived classes from base ActionResul class.

E - Return Action Results by a Method from Controller base class

In most cases, a controller action returns a ViewResult. For example, the Index controller action in Listing 2 returns a ViewResult.

using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    public class CustomerController : Controller
    {
        public ActionResult Details(int? id)
        {
            if (!id.HasValue)
                return RedirectToAction("Index");
            return View();
        }
        public ActionResult Index()
        {
            return View();
        }
    }
}

When an action returns a ViewResult, HTML is returned to the browser. The Index() method returns a view named Index to the browser.

Notice that the Index() action does not return a ViewResult(). Instead, the View() method of the Controller base class is called. Normally, you do not return an action result directly. Instead, you call one of the following methods of the Controller base class:

  1. View - Returns a ViewResult action result.
  2. Redirect - Returns a RedirectResult action result.
  3. RedirectToAction - Returns a RedirectToRouteResult action result.
  4. RedirectToRoute - Returns a RedirectToRouteResult action result.
  5. JSON - Returns a JsonResult action result.
  6. JavaScriptResult - Returns a JavaScriptResult.
  7. Content - Returns a ContentResult action result.
  8. File - Returns a FileContentResult, FilePathResult, or FileStreamResult depending on the parameters passed to the method.

So, if you want to return a View to the browser, you call the View() method. If you want to redirect the user from one controller action to another, you call the RedirectToAction() method. See below table:

Controller Action Result (1), in ASP.NET MVC

Action method in ASP.NET MVC

In the case, under the controller, if we use keyword this to represent the controller before the View(), we can the methods from the controller, such as View, then the IntelliSense will tell us if this method type belongs to any of class derived from base ActionResult class:

Controller Action Result (1), in ASP.NET MVC

or not:

Controller Action Result (1), in ASP.NET MVC

References


Similar Articles