Introduction To ActionResult Method in MVC

Introduction

In this article, I am describing the ActionResult method used in Model View Controller (MVC) based ASP.NET Web Applications. The ActionResult method is derived from the ActionResult class. The ActionResult Class encloses the output of the action method and does the operation. ActionResult is a very valuable aspect of MVC.

ActionResult

The ActionResult method works as a return type of any controller method in the MVC. It acts as the base class of Result classes. It is used to return the models to the Views, and file streams, and also redirect to the controllers. It is the responsibility of the Controller that connect the component.

The following code is an example of an ActionResult method.

public ActionResult Index()
{
    ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
   
    return View();
}
   
public ActionResult About()
{
    ViewBag.Message = "Your app description page.";
   
    return View();
}

You can use many DerivedTypes of the ActionResult to return the result that works individually for an appropriate view. If you want to use any of the DerivedTypes of ActionResult then simply hover any of them over an ActionResult method.

You can see all the DerivedTypes of the Action method using the following procedure.

Step 1. Install the Productivity Power Tools for Visual Studio.

Step 2. Restart the Visual Studio (if open).

Step 3. Just hover the mouse on the ActionResult. You will see the following image.

ActionResult

You can watch the ViewBag properties to see the strongly typed collection of the objects. For that use the following procedure.

Step 1. Insert the breakpoint at the end of the ActionResult.

Breakpoint Insertion

Step 2. Debug the application and just right-click on the ViewBag and click on "AddWatch".

AddWatch

Now you can see and expand the properties.

ViewBag Add Watch

Action Methods and Routing

In the MVC app, both the ActionResult method and routing work together to perform the operation. You can see the "Global.asax.cs" file for the defined routing patterns that connect the Action methods and controllers with the HTTP requests.

The predefined routing pattern of the Global. asax.cs file is as follows.

{Controller}/{action}/{id}
Just as in the following code:
public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

Now let's have a look at the code above in brief.

  • controller: In the code above, you can see that the controller name is Home. It is used to map the Uniform Resource Locator (URL) of the controller class. You can also see the URL code above. The Home Controller is used for the controller in the code above.
  • action: action is used to map the individual method that exists in the controller. Here in the code above the Index() action method exists in the HomeController.
  • id: id maps to an identifier. It is the method parameters of the controller action. Normally, the controller methods use this ID to query for a single record that matches with the ID to return to the view.

Action methods also nominate which result will be returned, HttpNotFoundResult, or HttpStatusCodeResult. In the Controller class, all the action methods defined have normally a corresponding view in the Views folder. For example, here in the previous code the Index() and About() action methods have their View in the Views folder. You can see that in the following image.

Solution Explorer

Summary

So far in this article, I described the ActionResult method of MVC-based ASP.NET Web Application. You can say that it is a very important part of the MVC application. Thanks for reading.


Similar Articles