MVC Life Cycle – 6th Part

Hi Friends,

In this section, we will continue from the last section and the see the next segment of Action Execution Process. Here, in this case we will see Filters in action. Therefore, before the action method chosen, the Authentication and Authorization filter runs. Filters are nothing but MVC components to inject extra logic in between the MVC pipeline.Therefore, first authentication filter runs and if it succeeds then it will go to Authorization filter otherwise it will again ask user to re-authenticate. However, authorization is the filter which says are you allowed to access particular thing or not.

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

Model Binders are also implemented via IModelBinder interface. This Interface also implements one method called BindModel. As we know that Model Binders run in between of different filters. Hence, it would be good idea to talk about filters in detail.

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

However, the code is really simple and self explanatory. Then, I have modified the controller code. Here, I have just added the filters attribute.

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.


I hope you would have liked this discussion. Thanks for joining me.

Code Download Link:-

Rahul Sahay
Happy Coding