ASP. net mvc Framework includes several action filters:
- Outputcache-caches the output of controller actions within the specified time.
- Handleerror-handling of controller action errors.
- Authorize-Restrict access permissions through specified users or roles.
Different types of filters
ASP. net mvc Framework supports four different types of filters:
- Authorization Filters-Implementation
Iauthorizationfilter interface attributes.
- Action Filters-Implementation
Iactionfilter interface attributes.
- Result Filters-Implementation
Iresultfilter interface attributes.
- Exception Filters-Implementation
Iexceptionfilter InterfaceAttribute.
Filters are executed in the order listed above. For example, the authorization filter is always executed at the beginning, and the exception filter is always executed at the end.
Authorization filter is used to verify and authorize the Controller action. For example, authorize filter is an example of filter verification;
Action filter contains logic used before or after the action is executed. For example, you can use an action filter to modify the View data returned by an action;
Result filter contains some logic for the view result of the action before and after execution. For example, you can modify a view result before the view is displayed in the browser;
Exception action is used to handle exception information. You can also use the exception filter to record error logs.
These filter types are executed according to the specified sequence. to control their execution sequence, you need to set the filter order attribute.
The base classes of these classes are system. web. MVC. filterattribute class. to implement a specific filter type, you can create a class that inherits the class and implement one or more iauthorization, iactionfilter, iresultfilter, and predictionfilter interfaces.
Actionfilterattribute base class
To make it easier for users to create a custom action filter, ASP. net mvc Framework provides a base class of actionfilterattribute, which implements the iactionfilter and iresultfilter interfaces and inherits the filter class.
The terms here are not exactly the same. Technically, this class inherits actionfitlerattribute and implements the action filter and result filter interfaces at the same time. However, in a loose sense, Asp. in the. net mvc Framework, any type that implements the filter is action filter.
The actionfilterattribute class can be rewritten using the following methods:
- onactionexecuting-called before the Controller action is executed
- onactionexecuted-called after the Controller action is executed
- onresultexecuting-called before the Controller action result is executed
- onresultexecuted-called after the Controller action result is executed