pages

Thursday 28 September 2017

Filter Attributes in MVC

An action filter is an attribute that can be applied to an Action or the controller.

Types of filters in MVC:

1.Authorization filters
2.Action filters
3.Result filters
4.Exception filters


Filters in MVC execute in the same order as mentioned above.

Authorization filters always execute before action filters and exception filters always execute at last.
The base class for all action filters is the FilterAttribute class which is in System.Web.Mvc namespace
The base class which is ActionFilterAttribute has the following methods which we can override:
· OnActionExecuting – This method is called before a controller action is executed.
· OnActionExecuted – This method is called after a controller action is executed.
· OnResultExecuting – This method is called before a controller action result is executed.
· OnResultExecuted – This method is called after a controller action result is executed.