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, file streams, and also redirect to the controllers. It is the responsibility of the Controller that connects the component.
 
The following code is an example of an ActionResult method:
  1. public ActionResult Index()  
  2. {  
  3.     ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";  
  4.    
  5.     return View();  
  6. }  
  7.    
  8. public ActionResult About()  
  9. {  
  10.     ViewBag.Message = "Your app description page.";  
  11.    
  12.     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 2: Just hover the mouse on the ActionResult. You will see the following image:
 
Derived Types of 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:
  1. {Controller}/{action}/{id}  
  2. Just as in the following code:  
  3. public class RouteConfig  
  4. {  
  5.     public static void RegisterRoutes(RouteCollection routes)  
  6.     {  
  7.         routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  8.    
  9.         routes.MapRoute(  
  10.             name: "Default",  
  11.             url: "{controller}/{action}/{id}",  
  12.             defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }  
  13.         );  
  14.     }  

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