Filters In ASP.NET MVC

Security of the web application is very important to ensure valuable information is protected and not accessible to unauthorized user or entity . There are lots of techniques to maintain the security of the application which depends on security type scenario. So in this article we learn in brief about the ASP.NET MVC filters which are useful to maintain the security during the request processing.

Following are the filters provided by ASP.NET MVC to check the business logic and validation before and after executing the action methods or view results .
  • Authorization filters
  • Action filters
  • Result filters
  • Exception filters
Authorization filters
Used to authenticate request before executing the action method to ensure the request is authenticated and genuine. Authorization filter uses IAuthorizationFilter interface in which authorization filter methods are defined.
 Authorization filters has following built in filters
  1. Authorize
  2. RequireHttps
Action filters

Action filter useful to take some action before and after executing the action methods, This action filter implements the IActionFilter interface which includes the following methods .
  1. OnActionExecuting 
  2. OnActionExecuted
Result filters

Result filter useful to perform some action before and after executing view result, This action filter implements the IResultFilter interface which includes the following methods .
  1. OnResultExecuting 
  2. OnResultExecuted 
Result  filter has built in OutputCache filter.

Exception filters  

Exception filter responsible to manage unhanded exceptions which are occurred during the execution of ASP.NET MVC pipeline request . This filter is very useful to capture and log the exception details wherever you wants. This filter implements the IExceptionFilter interface also it has also  built in filter named HandleError.

These filter are executed in the following sequence
  1. Authorization filters 
  2. Action filters 
  3. Response filters 
  4. Exception filters 
Key Points
  • Filters are used to check some business logic before and after executing the action methods or view result. 
  • Each filter implements the its own interface methods.
  • Filters can be applied on Controller level , action method level as well as globally using filterconfig class
  • We can apply multiple filters on single controller or action method
  • If given ASP.NET MVC filter does not fulfill the our requirement then we can also create the custom filters using  FilterAttribute class.
  • Usually filters are registered in Global.asax file 
 I hope you have got brief understanding about the ASP.NET MVC filters , In my next article we will learn each of the filter in details with real time examples.
I hope, this article is useful for all the readers. If you have any suggestions, please contact me.

Don't Forget To 

Post a Comment

Protected by Copyscape
Copyright © Compilemode