This article is a mirror article of machine translation, please click here to jump to the original article.

View: 20928|Reply: 3

[ASP.NET] mvc custom filter ActionFilterAttribute class

[Copy link]
Posted on 1/6/2016 3:32:33 PM | | |

In case the filters introduced earlier don't meet the requirements, this custom filter should come in handy if you want to define your own processing logic before and after the behavior method is executed and returned. To customize a filter, you inherit the ActionFilterAttribute class, which is an abstract class that implements the IActionFilter and IResultFilter interfaces, mainly by rewriting the four virtual methods to achieve injection logic before and after the execution and return of the behavior method

method
parameter
description
OnActionExecuting
ActionExecutingContext
Execute before the behavioral method is executed
OnActionExecuted
ActionExecutedContext
Execute after the behavioral method is executed
OnResultExecuting
ResultExecutingContext
Execute before the behavior method returns
OnResultExecuted
ResultExecutedContext
Execute after the behavior method is returned

The four methods are executed in the order of OnActionExecuting>OnActionExecuted>OnResultExecuting>OnResultExecuted. The arguments of the above four methods are inherited from the ContollorContext class. For example, a custom filter is defined below



The behavioral methods for using it are defined below








Previous:Linode, a well-known hosting service provider, is suspected of being hacked, and users need to change their passwords as soon as possible
Next:Memcache looks at all key methods listed
 Landlord| Posted on 10/25/2016 10:00:36 AM |
 Landlord| Posted on 8/10/2019 10:16:41 PM |
.NET MVC filter execution order:

IAuthorizationFilter(OnAuthorization)----->IActionFilter(OnActionExecuting)---->

Controller Action()---->IActionFilter(OnActionExecuted) ---->IResultFilter(OnResultExecuting) ---->

view()---->IResultFilter(OnResultExecuted)

In addition, the same filter execution order: from top to bottom, proximity principle:




Filter2=>Filter1
Posted on 8/26/2019 1:24:19 PM |
Mark, learned
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com