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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using MVC_Life_Cycle.Utils;
-
- namespace MVC_Life_Cycle.Controllers
- {
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- return View();
- }
-
- public ActionResult About()
- {
- ViewBag.Message = "Your application description page.";
-
- return View();
- }
-
- public ActionResult About(string msg)
- {
- ViewBag.Message = "Your application description page.";
-
- return View();
- }
-
- public ActionResult Contact()
- {
- ViewBag.Message = "Your contact page.";
-
- return View();
- }
- }
- }
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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using MVC_Life_Cycle.Utils;
-
- namespace MVC_Life_Cycle.Controllers
- {
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- return View();
- }
-
- public ActionResult About()
- {
- ViewBag.Message = "Your application description page.";
-
- return View();
- }
-
- [HttpPost]
- public ActionResult About(string msg)
- {
- ViewBag.Message = "Your application description page.";
-
- return View();
- }
-
- public ActionResult Contact()
- {
- ViewBag.Message = "Your contact page.";
-
- return View();
- }
- }
- }
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.
- using System.Reflection;
- using System.Web.Mvc;
-
- namespace MVC_Life_Cycle.Utils
- {
- public class IsValidRequest :ActionMethodSelectorAttribute
- {
-
- public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
- {
- return false;
- }
- }
- }
I have also modified the controller code to check whether request is valid or not.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using MVC_Life_Cycle.Utils;
-
- namespace MVC_Life_Cycle.Controllers
- {
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- return View();
- }
-
- [IsValidRequest]
- public JsonResult About()
- {
- return Json("{Msg: From a Custom Action Invoker!}", JsonRequestBehavior.AllowGet);
- }
-
- public ActionResult About(string msg)
- {
- ViewBag.Message = "Your application description page.";
-
- return View();
- }
-
- public ActionResult Contact()
- {
- ViewBag.Message = "Your contact page.";
-
- return View();
- }
- }
- }
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.
- using System.Reflection;
- using System.Web.Mvc;
-
- namespace MVC_Life_Cycle.Utils
- {
- public class IsValidRequest :ActionMethodSelectorAttribute
- {
-
- public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
- {
- return true;
- }
- }
- }
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 .