Powered by Blogger.

Saturday, March 1, 2014

Filter Attribute in MVC



Here we will discuss about Filter Attribute in MVC.

ASP.NET MVC provides a way for us to do that by writing custom filters that can be used to avoid extra processing logic in the request-response cycle.
It will help us to use Some extra logic that is written once and can be reused across multiple controllers and/or actions.

Also you can check out:

Get only alphabet characters from string in asp.net

- Set Maximum length for multiline textbox in asp.net

- Close browser in button click in Asp.Net

The way we can put the pre-processing and post-processing logic is by decorating the actions with attributes which will invoke an attribute class implementing the logic inside Filter.

For example, when the user clicks any link, a request is routed to the respective controller, and the corresponding action method is called.

Structure will be like below

[Attribute1]
Controller1
{

[Attribute2]
Action()
{

}

}

Type of filters

Basicaly there are 4 types of filters are there in MVC.

1- Authorization filter
2- Action filter
3- Result filter
4- Exception filter

1- Authorization filter:
This filter provides authentication and authorization logic. It will be executed before the action gets executed.

To implement this,interface IAuthorizationFilter should be implemented by the custom attribute class.

2- Action filter:
This filter will be called before and after the action starts executing and after the action has executed.You can put your logic inside Filter.

Here to implement this IActionFilter filter interface need to be implemented.

This interface provides us two methods OnActionExecuting and OnActionExecuted. OnActionExecuting will call before the action gets executed and OnActionExecuted
will call after the action gets executed.

3- Result filter:
We can use this filter suppose we want some modification to be done in the action's result.To use this filter we need to implement the IResultFilter interface.

This interface provides us two methods i.e OnResultExecuting and OnResultExecuted.OnResultExecuting will call before the action result gets executed and OnResultExecuted will call after the action result gets executed.

4- Exception filter:
whenever a controller or action of the controller throws an exception that time this filter will invoke.

This is particularly useful when we need custom error logging.IExceptionFilter interface needs to call here. This interface will provide one method OnException.

Note: The order of execution will be like
     
      1- Authorization filter
      2- Action filter
      3- Result filter
      4- Exception filter

Built-in Attributes

ASP.NET MVC provides some of built in attribute classes Thease are as belows:

1- AuthorizeAttribute: MVC provides AuthorizeAttribute that is helpful in specifying our custom authorization rules.

2- ActionFilterAttribute: This is the built in implementation for both IActionFilter and IResultFilter.

3- HandleErrorAttribute: This is the built in implementation of IExceptionFilter which makes it easier to implement the exception handling.





0 comments

Post a Comment