Directory:
- Filter Introduction
- Filter category
- Custom filters and filtering features
- Direct short-circuit return content
- The difference between filter and middleware
- If you want a global log, do not use filters
Official Document Portal
1. Filter Introduction
No direct return, resource cache, action after the execution of the filter, the exception of custom processing, the results of custom processing and other operations can be used filter. And save a lot of code.
Official explanation:filters in ASP. NET Core mvc allow you to run code before or after certain stages in the request processing pipeline
2. Filter category [Direct citation official explanation]
Each filter type executes at different stages in the filter pipeline.
-
authorization filter runs first and is used to determine whether the current user is authorized for the current request. If the request is unauthorized, they may cause the pipeline to short-circuited.
-
Resource filter is the first to process a request after authorization Span style= "COLOR: #000000" > filter . They can run the code before the rest of the filter pipeline and run the code after the rest of the pipeline is complete. For performance reasons, they can be used to implement caching or otherwise short-circuit the filter pipeline. Because they run before the model is bound, they are useful for anything that needs to affect the binding of the model.
-
action filter You can run code immediately before and after a single action method is called. They can be used to manipulate parameters that are passed to the operation and return results from the operation.
-
exception filter An unhandled exception that occurs before the global policy is applied to anything that has been written to the response body.
-
result filter You can run code immediately before and after a single action result is executed. They run
only if the action method executes successfully and is useful for logic that must be executed around the view or formatter.
3. Custom Filters
Custom filters in two ways. This is a separate action filter.
A. Inherit iactionfilter to implement its own filter class and inject it into the SARTUP,MVC service. Filter all the time, before and after any action is executed
Iactionfilter inheritance needs to implement two methods Onactionexecuted,onactionexecuting, respectively, before and after the execution of the action
First: Custom Filter class
Public classMyactionfilter:iactionfilter { Public voidonactionexecuted (ActionExecutedContext context) {vars ="filter_onactionexecuted"; } Public voidonactionexecuting (ActionExecutingContext context) {vars ="filter_onactionexecuted"; } }
View Code
Second: Injection
Public void configureservices (iservicecollection services) { // ADD Framework services. Services. ADDMVC (options = options. Filters.add (new myactionfilter ()); });
View Code
B. Inherit attribute, Iactionfilter, with attribute feature identified to the place where you want to filter for local filtering (controller,action)
First: Custom Filter Features
Public classMyactionfilterattribute:attribute, Iactionfilter { Public voidonactionexecuted (ActionExecutedContext context) {vars ="attribute_onactionexecuted"; } Public voidonactionexecuting (ActionExecutingContext context) {vars ="attribute_onactionexecuting"; } }
View Code
Second: Labeling limits for single action filtering
[Myactionfilter] public iactionresult Index () { return View (); }
View Code
Second: Labeling limits for single controller filtering
[Myactionfilter] Public classHomecontroller:controller { PublicIactionresult Index () {returnView (); } PublicIactionresult About () {viewdata["Message"] ="Your Application description page."; returnView (); } PublicIactionresult Contact () {viewdata["Message"] ="Your Contact page."; returnView (); } PublicIactionresult Error () {returnView (); } }
View Code
Note: When you implement a custom filter injection, you implement the characteristics of the category filter. Then the execution order is the global filter onactionexecuting, then the feature onactionexecuting, and then the feature onactionexecuted, Finally, the filter onactionexecuted.
4. Direct short-circuit return content
Public classMyactionfilterattribute:attribute, Iactionfilter { Public voidonactionexecuted (ActionExecutedContext context) {context. Result=NewContentresult () {Content="Resource Unavailable-header should not being set" }; } Public voidonactionexecuting (ActionExecutingContext context) {vars ="attribute_onactionexecuting"; } }
View Code
As long as the return contentresult is shorted, all subsequent logic will not be processed.
5. The difference between filter and middleware
The filter remainder middleware is very similar. Refers to the article address. The difference is that, as a two-AOP tool, the filter is more business-focused, it focuses on the application itself, such as you see ActionFilter
and ResultFilter
, it is directly and your action,actionresult interaction, is not close to you feel, Then I have some example of the output of my format, the ViewModel of my request for data validation, it must be using filter is undoubtedly. It's part of MVC, it can intercept some information about your action context, and middleware doesn't have that ability.
6. If you want a global log, do not use a filter
The official document says. It is not recommended to use filters as logging
Reason: avoid creating and using filters that are purely for logging purposes, because the built-in framework logging feature may already provide what you need. If you want to add logging to a filter, you should focus on the filter-specific business area problem or behavior, rather than the MVC action or other framework events.
12 ASP. NET Core Example-filter