IAlwaysRunResultFilter

1、IResourceFilter设置context.Result后,程序不在往后执行,可以利用这个特性来做页面缓存,查看
https://www.cnblogs.com/handsomeziff/p/16215572.html

当需要在context.Result后对结果在做一些处理,可以利用IAlwaysRunResultFilter

2、新建类CustomAlwayRunResultFilterAttribute继承Attribute, IAlwaysRunResultFilter

复制代码
 1 using Microsoft.AspNetCore.Mvc.Filters;
 2 
 3 namespace Project6.Utility.Filters
 4 {
 5     public class CustomAlwayRunResultFilterAttribute : Attribute, IAlwaysRunResultFilter
 6     {
 7         public void OnResultExecuted(ResultExecutedContext context)
 8         {
 9             Console.WriteLine("OnResultExecuted");
10         }
11 
12         public void OnResultExecuting(ResultExecutingContext context)
13         {
14             Console.WriteLine("OnResultExecuting");
15         }
16     }
17 }
View Code
复制代码

3、新建控制器CustomAlwaysRunResultController

复制代码
 1 using Microsoft.AspNetCore.Mvc;
 2 using Project6.Utility.Filters;
 3 
 4 namespace Project6.Controllers
 5 {
 6     public class CustomAlwaysRunResultController : Controller
 7     {
 8         [CustomResourceFilterAttribute]
 9         [CustomAlwayRunResultFilterAttribute]
10         public IActionResult Index()
11         {
12             return Json(new
13             {
14                 Id = 123,
15                 Name = "net"
16             });
17         }
18     }
19 }
View Code
复制代码

4、在各个方法处设置断点,执行/CustomAlwaysRunResult

可以发现在程序在返回结果前都要执行CustomAlwayRunResultFilterAttribute里面的OnResultExecuting和OnResultExecuted

posted @   ziff123  阅读(100)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示