ToB企服应用市场:ToB评测及商务社交产业平台

标题: ASP.NET Core 6.0 Filter 生命周期 [打印本页]

作者: 美丽的神话    时间: 2024-7-28 07:04
标题: ASP.NET Core 6.0 Filter 生命周期
Filter生命周期

在 FourthController 控制器新建 Index5 方法,在对应的过滤器中设置断点。 
  1.         [Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme, Policy = "rolePolicy")]
  2.         [CustomCacheResourceFilter]
  3.         [TypeFilter(typeof(CustomLogActionFilterAttribute))]
  4.         [TypeFilter(typeof(CustomResultFilterAttribute))]
  5.         [TypeFilter(typeof(CustomAlwaysRunResultFilterAttribute))]
  6.         [TypeFilter(typeof(CustomExceptionFilterAttribute))]
  7.         public IActionResult Index5()
  8.         {
  9.             return View();
  10.         }
复制代码
参考之前关于过滤器的文章:
https://blog.csdn.net/nmmking/article/details/139025585
https://blog.csdn.net/nmmking/article/details/139025585https://blog.csdn.net/nmmking/article/details/139021374
https://blog.csdn.net/nmmking/article/details/139021374https://blog.csdn.net/nmmking/article/details/139023237
https://blog.csdn.net/nmmking/article/details/139023237https://blog.csdn.net/nmmking/article/details/138615697
https://blog.csdn.net/nmmking/article/details/138615697分别把6个Filter全部注册到某一个方法,访问 FourthController  的 Index5 :


多个相同的Filter不同注册效果

分别定义三个Filter采用全局注册、控制器注册、Action注册。
  1.     public class CustomActionActionFilterAttribute : ActionFilterAttribute
  2.     {
  3.         public override void OnActionExecuting(ActionExecutingContext context)
  4.         {
  5.             Console.WriteLine("CustomControllerActionFilterAttribute.OnActionExecuting");
  6.         }
  7.         public override void OnActionExecuted(ActionExecutedContext context)
  8.         {
  9.             Console.WriteLine("CustomControllerActionFilterAttribute.OnActionExecuted");
  10.         }
  11.     }
复制代码
  1.     public class CustomControllerActionFilterAttribute : ActionFilterAttribute
  2.     {
  3.         public override void OnActionExecuting(ActionExecutingContext context)
  4.         {
  5.             Console.WriteLine("CustomControllerActionFilterAttribute.OnActionExecuting");
  6.         }
  7.         public override void OnActionExecuted(ActionExecutedContext context)
  8.         {
  9.             Console.WriteLine("CustomControllerActionFilterAttribute.OnActionExecuted");
  10.         }
  11.     }
复制代码
  1.     public class CustomGlobalActionFilterAttribute : ActionFilterAttribute
  2.     {
  3.         public override void OnActionExecuting(ActionExecutingContext context)
  4.         {
  5.             Console.WriteLine("CustomGlobalActionFilterAttribute.OnActionExecuting");
  6.         }
  7.         public override void OnActionExecuted(ActionExecutedContext context)
  8.         {
  9.             Console.WriteLine("CustomGlobalActionFilterAttribute.OnActionExecuted");
  10.         }
  11.     }
复制代码
以下就是默认实验序次:





可以通过Order 来控制实验序次,Order的值越小,就越先实验:
[CustomControllerActionFilterAttribute(Order =20)]
[CustomActionActionFilterAttribute(Order =-99)]


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4