EnjoySharePoint YouTube Channel

Filter Attribute in MVC

Here we will discuss 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.
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.
The structure will be like below
Type of filters
Basically, 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 the built-in attribute classes These are as below:
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.
Check out Best Alternative to InfoPath -> Try Now
You May Also like the Following SharePoint Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →