----------------------------------------------
Note : The ActionFilterAttribute class implements both Iactionfilter and Iresultfilter interfaces. is an abstract class that requires you to provide an implementation (derived class).
Authorizeattibute and Handleerrorattribute, however, contain useful features that can be used without having to create derived classes.
Note : If a custom base class is defined for a controller, then any filter on the basis class will affect its derived class.
----------------------------------------------
First, the use of filters
1. Filter : The request processing that injects additional logic into the MVC framework. is the. Net annotation attribute (Attribute), which adds additional steps to the request processing pipeline.
2. Annotation Properties : a special. NET class that is derived from System.Attribute. Can be attached to other code elements (classes, methods, properties, fields, and so on).
The goal is to embed additional information into the compiled code to read back the information at run time.
In C #, the annotation properties are appended with the atttibute end, square brackets [], which can be assigned to their properties (public) with named parameter syntax.
When using the annotation attribute, the trailing attibute is omitted: authorizeattibute->[authorize]
3. Cross-focus : Can be used for the entire application, but not the ability to rotate in a local location, or will break the focus on the separation mode (login, authorization, caching, etc.).
Filter types for the MVC framework
| Filter type |
Interface |
Default implementation |
Describe |
| Authorization (Certified) |
Iauthorizationfilter |
Authorizeattibute |
Run first, before any other filter or action method |
| Action (Action) |
Iactionfilter |
ActionFilterAttribute |
Run before and after the action method |
| Result (results) |
Iresultfilter |
Actionfiterattribute |
Run before and after the action result is executed |
| Exception (Exception) |
Iexceptionfiter |
Handleerrorattribute |
Run only if another filter, action method, or action result throws an exception |
4. Built-in authorization filter: (Authorizeattribute)
(1) Attribute Users: String A comma-delimited list of user names that allow these users to access the action method
(2) attribute Roles: String A comma-delimited list of roles. In order to access the action method, the user must be at least one of these roles
5. Exception filter: (Handleerrorattribute)
(1) The main application of abnormal filter : 1. Log the exception, 2. Display the appropriate message to the user.
Exceptioncontext Property
| Name |
Type |
Describe |
| Controller |
Controllerbase |
Returns the requested controller object |
| HttpContext |
HttpContextBase |
Provides access to the details of the request, as well as access to the response |
| Ischildaction |
bool |
Child action judgment |
| RequestContext |
RequestContext |
Provides access to HttpContext and routed data |
| Routedata |
Routedata |
Return the requested route data |
Exceptioncontext Additional Properties
| Name |
Type |
Describe |
| Actiondescriptor |
Actiondescriptor |
Provides details of the action method |
| Result |
ActionResult |
Results for the action method |
| Exception |
Exception |
Exception not handled |
| Exceptionhandled |
bool |
Returns true if another filter has marked this exception as handled |
Handleerrorattribute Property
| Name |
Type |
Describe |
| Exceptiontype |
Type |
The type of exception handled by the filter. It also handles exception types that are inherited by a given value, but ignores all other types. Default is System.Exception, which handles all standard exceptions by default |
| View |
String |
The name of the view that the filter renders. The default is error. Default Render/view/<cuttentcontrollername>/error.html or/view/shared/error.htm |
| Master |
String |
The name of the layout used when rendering the view of this filter. Default Layout page |
(2) use the built-in exception filter to increase after the 〈pages〉 node in the 〈system.web〉 node in Web. config
( / pages〉 〈customerrors mode= "on" defaultredirect= "/content/rangeerrorpage.html"/〉〈 / system.web〉
Mode: The default is RemoteOnly, which means that during development, Handleerrorattribute will not intercept the exception, but only after deployment and when a request is made from another computer.
Deaultredirect: Specifies a content page that is used when the exception message cannot be displayed in other cases.
16th Chapter Filter