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 />";
- }
- }
- }


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