MVC Filters 1
MVC Filters 1
MVC Filters 1
ASP.NET MVC Filter is a custom class where you can write custom logic to
execute before or after an action method executes. Filters can be applied to
an action method or controller in a declarative or programmatic way.
Declarative means by applying a filter attribute to an action method or
controller class and programmatic means by implementing a corresponding
interface.
ASP.NET MVC provides a simple way to inject your piece of code or logic
either before or after an action is executed. This is achieved by decorating
the controllers or actions with ASP.NET MVC attributes or custom attributes.
An attribute or custom attribute implements the ASP.NET MVC filters (filter
interface) and can contain your piece of code or logic. You can make your
own custom filters or attributes either by implementing ASP.NET MVC filter
interface or by inheriting and overriding methods of ASP.NET MVC filter
attribute class if available.
Typically, Filters are used to perform the following common functionalities in your ASP.NET MVC
application.
1. Custom Authentication
2. Custom Authorization(User based or Role based)
3. Error handling or logging
4. User Activity Logging
5. Data Caching
Types of Filters
Authorization Filters
The ASP.NET MVC Authorize filter attribute implements the IAuthorizationFilter interface. The
definition of this interface is given below-
Action Filters
Action filters are executed before or after an action is executed. The IActionFilter interface is used
to create an Action Filter which provides two methods OnActionExecuting and OnActionExecuted
which will be executed before or after an action is executed respectively.
Result Filters
Result filters are executed before or after generating the result for an action. The Action Result
type can be ViewResult, PartialViewResult, RedirectToRouteResult, RedirectResult, ContentResult,
JsonResult, FileResult and EmptyResult which derives from the ActionResult class. Result filters
are called after the Action filters. The IResultFilter interface is used to create an Result Filter which
provides two methods OnResultExecuting and OnResultExecuted which will be executed before
or after generating the result for an action respectively.
Exception Filters
Exception filters are executed when exception occurs during the actions execution or filters
execution. The IExceptionFilter interface is used to create an Exception Filter which provides
OnException method which will be executed when exception occurs during the actions execution
or filters execution.
All ASP.NET MVC filter are executed in an order. The correct order of execution is given below:
1. Authentication filters
2. Authorization filters
3. Action filters
4. Result filters
Configuring Filters
You can configure your own custom filter into your application at following three levels:
1. Global level
By registering your filter into Application_Start event of Global.asax.cs file with the help of
FilterConfig class.
1. protected void Application_Start()
2. {
3. FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
4. }
2. Controller level
By putting your filter on the top of the controller name as shown below-
1. [Authorize(Roles="Admin")]
2. public class AdminController : Controller
3. {
4. //
5. }
3. Action level
By putting your filter on the top of the action name as shown below-
1. public class UserController : Controller
2. {
3. [Authorize(Users="User1,User2")]
4. public ActionResult LinkLogin(string provider)
5. {
6. // TODO:
7. return View();
8. }
9. }