Action Filters In ASP.NET MVC
Action Filters - Introduction
ASP.NET MVC is a web development framework that is based on Model-View-Controller architectural design pattern.Filters in MVC are one of those feature.Filters are some of the best extension points provided in MVC.Mainly ,ASP.NET MVC provides Action Filters for executing filtering logic either before or after an action method is called. Action Filters are some times custom attributes that provide to add pre-action and post-action behavior to the controller's action methods.
Basically the filters in ASP .NET MVC provides a set features .They are the following :
- Custom Authentication
- User based or Role based custom Authorization
- Error handling or logging
- User Activity Logging
- Data Caching
- Data Compression
Action filter is one type of filter.There are basically five types of filters are available in ASP.NET MVC framework. Then can see what are they :
- Authentication filters
- Authorization filters
- Action filters
- Result filters
- Exception filters
When we create Action Filters, it implements an interface called IActionFilterand also we want to declare and implement OnActionExecuting and OnActionexecuted methods.
The following code is usefull for implementing this.
LikeWise MVC also provides the following types of action filters :
- Authorization filter
- Action filter
- Result filter
- Exception filter
By building Action Filter first we need to create a new project in ASP .NET MVC 4.We can use the Internet or Intranet template.
If a Filters folder does not exist in our project , create one in the project.Also add a class to the Filters folder.Then we can implement it.
In ASP.NET MVC, application of ActionFilter - filter method in the Action method, as shown in the diagram below also its execution process: