MVC Life Cycle - Part 5

Before reading this article, I highly recommend you read my previous parts:

In this section, we will look at Action Execution event in the MVC life Cycle. However, I strongly recommend you refer to the previous articles on the same topic. Once the controller gets initialized then action invoker has to takethe decision saying which method to invoke, and this piece is done by Action Invoker in the life cycle. Action Invoker based on different method signatures present in the controller, identifies the exact method to be invoked.
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Web;    
  5. using System.Web.Mvc;    
  6. using MVC_Life_Cycle.Utils;    
  7.     
  8. namespace MVC_Life_Cycle.Controllers    
  9. {    
  10.     public class HomeController : Controller    
  11.     {    
  12.         public ActionResult Index()    
  13.         {    
  14.             return View();    
  15.         }    
  16.     
  17.         public ActionResult About()    
  18.         {    
  19.             ViewBag.Message = "Your application description page.";    
  20.     
  21.             return View();    
  22.         }    
  23.     
  24.         public ActionResult About(string msg)    
  25.         {    
  26.             ViewBag.Message = "Your application description page.";    
  27.     
  28.             return View();    
  29.         }    
  30.     
  31.         public ActionResult Contact()    
  32.         {    
  33.             ViewBag.Message = "Your contact page.";    
  34.     
  35.             return View();    
  36.         }    
  37.     }    
  38. }    
In the above snippet, there are two 'About' methods. In this case, it has become ambiguous for action invoker to figure out how to choose which method to pick and hencehas produced the below error.
 
 
However, this can be easily fixed by applying any HTTP Verbs or Action Selectors like shown below.
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Web;    
  5. using System.Web.Mvc;    
  6. using MVC_Life_Cycle.Utils;    
  7.     
  8. namespace MVC_Life_Cycle.Controllers    
  9. {    
  10.     public class HomeController : Controller    
  11.     {    
  12.         public ActionResult Index()    
  13.         {    
  14.             return View();    
  15.         }    
  16.     
  17.         public ActionResult About()    
  18.         {    
  19.             ViewBag.Message = "Your application description page.";    
  20.     
  21.             return View();    
  22.         }    
  23.     
  24.         [HttpPost]    
  25.         public ActionResult About(string msg)    
  26.         {    
  27.             ViewBag.Message = "Your application description page.";    
  28.     
  29.             return View();    
  30.         }    
  31.     
  32.         public ActionResult Contact()    
  33.         {    
  34.             ViewBag.Message = "Your contact page.";    
  35.     
  36.             return View();    
  37.         }    
  38.     }    
  39. }   
However, there could be a scenario wherein multiple GET requests are required and action should be taken on certain additional conditions, be it any thing. In that case, we need to make use of custom action invoker wherein it will check whether the requested method is valid or not and then based on that, it will take decision as shown below.
  1. using System.Reflection;    
  2. using System.Web.Mvc;    
  3.     
  4. namespace MVC_Life_Cycle.Utils    
  5. {    
  6.     public class IsValidRequest :ActionMethodSelectorAttribute    
  7.     {    
  8.         //Currently just hardcoded to false.    
  9.         public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)    
  10.         {    
  11.             return false;    
  12.         }    
  13.     }    
  14. }    
I have also modified the controller code to check whether request is valid or not.
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Web;    
  5. using System.Web.Mvc;    
  6. using MVC_Life_Cycle.Utils;    
  7.     
  8. namespace MVC_Life_Cycle.Controllers    
  9. {    
  10.     public class HomeController : Controller    
  11.     {    
  12.         public ActionResult Index()    
  13.         {    
  14.             return View();    
  15.         }    
  16.     
  17.         [IsValidRequest]    
  18.         public JsonResult About()    
  19.         {    
  20.             return Json("{Msg: From a Custom Action Invoker!}", JsonRequestBehavior.AllowGet);    
  21.         }    
  22.     
  23.         public ActionResult About(string msg)    
  24.         {    
  25.             ViewBag.Message = "Your application description page.";    
  26.     
  27.             return View();    
  28.         }    
  29.     
  30.         public ActionResult Contact()    
  31.         {    
  32.             ViewBag.Message = "Your contact page.";    
  33.     
  34.             return View();    
  35.         }    
  36.     }    
  37. }  
With the above change in place, when I run the program and click the about link, it will first check the attribute code and then accordingly make the decision as shown below.







And if I make the action invoker method check to return true like shown below, then it will produce the JSON Result.
  1. using System.Reflection;    
  2. using System.Web.Mvc;    
  3.     
  4. namespace MVC_Life_Cycle.Utils    
  5. {    
  6.     public class IsValidRequest :ActionMethodSelectorAttribute    
  7.     {    
  8.         //Currently just hardcoded to true.    
  9.         public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)    
  10.         {    
  11.             return true;    
  12.         }    
  13.     }    
  14. }    




I hope you have liked the discussion of MVC Action Invoker and how important it is to make the decision making process while selecting the required method inside the controller. In the coming section, we will see more abuot this. Until then stay tuned and happy coding.

Code Download
Link .