Before reading this article, I highly recommend you read the previous parts of the article series:
- MVC Life Cycle - Part 1
- MVC Life Cycle - Part 2
- MVC Life Cycle - Part 3
- MVC Life Cycle - Part 4
- MVC Life Cycle - Part 5
At this stage, all authentication and authorization got succeeded which means, it will move to next step of pipeline wherein action invoker will pick the method to execute, but before execution it needs to populate data for Model Binding. Model Binding is the process of taking data from different data sources say Form Data, Query-String etc and create objects for action method parameters. Once, the parameters get populated then another set of action filters get executed which is associated with controllers itself like OnActionExecuting or OnActionExecuted. Therefore, before actual method gets invoked OnActionExecuting filter will run and after that the later one. After this entire exercise, Action Method will finally get called. Now, Action Methods after running will return Action Results. Now, there are variety of Action Results like JSON-Result, View Result, etc. Also, based on this Action Result it will generate the actual HTTP-Response for the HTTP-Request. This is just the brief snapshot around the Action Execution.
Therefore ActionInvoker is basically responsible for generating response. Again like many other MVC components, ActionInvoker also implements IActionInvoker which has only one method InvokeAction. However, you can also extend the same and create your own ActionInvoker which handles the request in custom way. But, as I mentioned earlier ASP.NET MVC is really powerful framework where in all the concerns are already addressed. So, I don't see any reason for doing this. Now, let us talk about Model Binders in detail. Model Binders fetch data from Value Providers. Below, I have mentioned the list of the same.
- Query-Strings
- Form-Data
- Route-Data
- It could be any custom source as well
Types of Filters: (Mentioned in execution order),
| Types of Filters | Mentioned in execution order |
| Authentication Filter | IAuthenticationFilter |
| Authorization Filter | IAuthorizationFilter |
| Action Filter | IActionFilter |
| Result Filter | IResultFilter |
| Exception Filter | IExceptionFilter |
Also, it is important to understand that filters can be applied at different levels. We can apply filters @Controller Method level or Controller level or Application level. This is why filters are really important component of MVC. We usually use filters in various scenarios either for logging or doing some business validations or anything which requires custom handling of your scenario. Now, let us quickly jump at demo. Below, I have pasted the custom filter code:
- using System.Web;
- using System.Web.Mvc;
- namespace MVC_Life_Cycle.CustomFilter
- {
- //Filter attribute is there to make the same accessible as attributes inside controller's methods
- //IActionFilter offcourse gives two pre and post execution methods.
- public class CustomFilter :FilterAttribute, IActionFilter
- {
- public void OnActionExecuting(ActionExecutingContext filterContext)
- {
- HttpContext.Current.Application["Filter"] += "Filter 1 Executing, <br />";
- }
- public void OnActionExecuted(ActionExecutedContext filterContext)
- {
- HttpContext.Current.Application["Filter"] += "Filter 1 Executed, <br />";
- }
- }
- public class CustomFilter2 : FilterAttribute, IActionFilter
- {
- public void OnActionExecuting(ActionExecutingContext filterContext)
- {
- HttpContext.Current.Application["Filter"] += "Filter 2 Executing, <br />";
- }
- public void OnActionExecuted(ActionExecutedContext filterContext)
- {
- HttpContext.Current.Application["Filter"] += "Filter 2 Executed, <br />";
- }
- }
- public class CustomFilter3 : FilterAttribute, IActionFilter
- {
- public void OnActionExecuting(ActionExecutingContext filterContext)
- {
- HttpContext.Current.Application["Filter"] += "Filter 3 Executing, <br />";
- }
- public void OnActionExecuted(ActionExecutedContext filterContext)
- {
- HttpContext.Current.Application["Filter"] += "Filter 3 Executed, <br />";
- }
- }
- public class CustomFilter4 : FilterAttribute, IActionFilter
- {
- public void OnActionExecuting(ActionExecutingContext filterContext)
- {
- HttpContext.Current.Application["Filter"] += "Filter 4 Executing, <br />";
- }
- public void OnActionExecuted(ActionExecutedContext filterContext)
- {
- HttpContext.Current.Application["Filter"] += "Filter 4 Executed, <br />";
- }
- }
- public class CustomFilter5 : FilterAttribute, IActionFilter
- {
- public void OnActionExecuting(ActionExecutingContext filterContext)
- {
- HttpContext.Current.Application["Filter"] += "Filter 5 Executing, <br />";
- }
- public void OnActionExecuted(ActionExecutedContext filterContext)
- {
- HttpContext.Current.Application["Filter"] += "Filter 5 Executed, <br />";
- }
- }
- }
However, the code is really simple and self explanatory. Then, I have modified the controller code. Here, I have just added the filters attribute.
- 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
- {
- [CustomFilter.CustomFilter]
- [CustomFilter.CustomFilter2]
- [CustomFilter.CustomFilter3]
- [CustomFilter.CustomFilter4]
- [CustomFilter.CustomFilter5]
- 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();
- }
- }
- }
Lastly, I have modified view to fetch the value and print the same.
With the above change in place, when I run the app, it will produce the following output.- @{
- ViewBag.Title = "Home Page";
- }
- <div class="jumbotron">
- <h1>Filter Execution Path</h1>
- <p class="lead">Refer MVC Life Cycle series at My View.</p>
- <p><a href="http://myview.rahulnivi.net" class="btn btn-primary btn-lg">Learn more »</a></p>
- </div>
- <div>
- <span>
- @{
- //Fetching the values from Application State
- @Html.Raw(HttpContext.Current.Application["Filter"]);
- }
- </span>
- </div>
- <div class="row">
- <div class="col-md-4">
- <h2>Getting started</h2>
- <p>
- ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that
- enables a clean separation of concerns and gives you full control over markup
- for enjoyable, agile development.
- </p>
- <p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301865">Learn more »</a></p>
- </div>
- <div class="col-md-4">
- <h2>Get more libraries</h2>
- <p>NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.</p>
- <p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301866">Learn more »</a></p>
- </div>
- <div class="col-md-4">
- <h2>Web Hosting</h2>
- <p>You can easily find a web hosting company that offers the right mix of features and price for your applications.</p>
- <p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301867">Learn more »</a></p>
- </div>
- </div>

I hope you have liked this discussion. Thanks for joining me.
Code Download Link.

Sibeesh VenuPosted Feb 1, 2016, 1:13 AM
Nice Share
Ankit BansalPosted Feb 1, 2016, 12:39 AM
nice one sir..
Saillesh PawarPosted Jan 31, 2016, 1:25 PM
Nice appreciate your hardwork
Jaco ZwartsPosted Jan 30, 2016, 8:22 AM
Nice thanks for sharing
Santhakumar MunuswamyPosted Jan 30, 2016, 2:16 AM
Thanks for nice share