Virtual Classes Logo          Virtual Classes
    Read Anytime,Anywhere

Filters In MVC

In ASP.NET MVC application, the incoming request first passes through the UrlRoutingModule. UrlRoutingModule parses the requested URL and then invokes the corresponding controller and action method. The controller will then render the appropriate view and send the response to the browser.
If we want to do some extra processing in the request-response life cycle to full fill the requirement.
e.g. you want to perform logic either before an action method is called or after an action method runs.
ASP.NET MVC has provideded filters that can be used to inject extra processing(to add pre-action and post-action )logic in the request-response life cycle.
In MVC,you can can put the pre-processing and post-processing logic by decorating the actions with attributes which will invoke an attribute class implementing the filter's logic.

Type of filters
1-Authorization filter:The ASP.NET MVC Authorize filter attribute implements the IAuthorizationFilter interface. The definition of this interface is given below-
public interface IAuthorizationFilter
void OnAuthorization(AuthorizationContext filterContext);
Authorization filter executes before the action gets executed.
Authorization filters are used to implement authentication and authorization for controller actions.

2-Action filter:Action filters contain logic that is executed before and after a controller action executes. You can use an action filter to modify the view data that a controller action returns.
Action filters are executed before or after an action is executed. Action filters implementes IActionFilter interface which provides two methods OnActionExecuting and OnActionExecuted which will be executed before or after an action is executed respectively.

public interface IActionFilter
void OnActionExecuting(ActionExecutingContext filterContext);
void OnActionExecuted(ActionExecutedContext filterContext);

3-Result filter:Result filters contain logic that is executed before and after a view result is executed.
For example, you might want to modify a view result right before the view is rendered to the browser. 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.
public interface IResultFilter
void OnResultExecuted(ResultExecutedContext filterContext);
void OnResultExecuting(ResultExecutingContext filterContext);

4-Exception filter:Exception filters are executed when exception occurs during the actions execution or filters execution. Exception Filter implements IExceptionFilter interface which provides OnException method which will be executed when exception occurs during the actions execution or filters execution.
public interface IExceptionFilter
void OnException(ExceptionContext filterContext);
Exception filters are the last type of filter to run. You can use an exception filter to handle errors raised by either your controller actions or controller action results. You also can use exception filters to log errors.

Order of Filter Execution-
Authorization filters are always executed first then action filters gets executed and then result filter and exception filters are always executed after every other type of filter.
If you want to control the order in which filters of the same type are executed then you can set a filter's Order property.