net core aop之IResultFilter使用方法
IResultFilter过滤器一个比较常见的使用场景就是接口返回Json格式的统一处理,实现方式首页创建一个类继承自Attribute,IResultFilter,并实现IResultFilter接口的OnResultExecuting、OnResultExecuted两个方法,OnResultExecuting方法在Action执行后执行,然后在执行OnResultExecuted方法。
具体代码实例如下:创建CustomResultFilterAttribute类继承自Attribute, IResultFilter方法,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class CustomResultFilterAttribute : Attribute, IResultFilter { public void OnResultExecuting(ResultExecutingContext context) { if (context.Result is JsonResult) { JsonResult result = (JsonResult)context.Result; context.Result = new JsonResult( new AjaxResult() { Success= true , Message= "Ok" , Data = result.Value }); } Console.WriteLine( "CustomResultFilterAttribute.OnResultExecuting" ); } public void OnResultExecuted(ResultExecutedContext context) { Console.WriteLine( "CustomResultFilterAttribute.OnResultExecuted" ); } } |
controller代码如下:
1 2 3 4 5 6 7 8 9 | [CustomResultFilter] public IActionResult Index2() { this ._Logger.LogInformation($ "index被执行了" ); return Json( new { Id=123, Name= "123" }); } |
返回结果如下:
猜您可能还喜欢
- net core+webapi+nginx windows 服务器部署(1346)
- .Nuget Packages 太占C盘,删除后可以放到其他盘(1237)
- ASP.NET Core 配置 Swagger 显示接口注释描述信息(1130)
- vue调用接口后获取不到后端返回的Header响应头(1001)
- .net core 系列实例开发教程-权限管理系统功能介绍(971)
- .net core 6.0 web API + SwaggerUI + IIS部署(946)
- .net core 实例教程(十二)配置启用Swagger中的【Authorize】按钮(861)
- .net core 实例教程(一)新建项目(829)
- .net core 实例教程(十四)配置 Swagger 显示接口注释描述信息及支持版本控制(805)
- .net 6中使用log4net写sqlserver数据库日志(779)
评论列表
发表评论
文章分类
文章归档
- 2025年3月 (1)
- 2024年6月 (2)
- 2024年5月 (2)
- 2024年4月 (4)
- 2024年3月 (30)
- 2024年1月 (4)
- 2023年12月 (2)
- 2023年11月 (4)
- 2023年10月 (4)
- 2023年9月 (6)
- 2023年3月 (2)
- 2023年2月 (1)
- 2023年1月 (1)
- 2022年12月 (1)
- 2022年9月 (21)
- 2022年8月 (10)
- 2022年7月 (3)
- 2022年4月 (1)
- 2022年3月 (13)
- 2021年8月 (1)
- 2021年3月 (1)
- 2020年12月 (42)
- 2020年11月 (7)
- 2020年10月 (5)
- 2020年8月 (1)
- 2020年6月 (1)
- 2020年3月 (2)
- 2019年12月 (8)
- 2019年11月 (3)
- 2019年9月 (1)
- 2019年4月 (1)
- 2019年3月 (6)
- 2019年2月 (1)
- 2018年7月 (7)
阅读排行
- 1.asp.net mvc内微信pc端、H5、JsApi支付方式总结(5730)
- 2.Windows 10休眠文件更改存储位置(3251)
- 3.各大搜索网站网站收录提交入口地址(3226)
- 4.ECharts仪表盘实例及参数使用详解(3122)
- 5.windows 10安装myeclipse 10破解补丁cracker.jar、run.bat闪退解决办法(3046)
- 6.HTML5 WebSocket与C#建立Socket连接实现代码(2916)
- 7.华为鸿蒙系统清除微信浏览器缓存方法(2835)
- 8.CERT_HAS_EXPIRED错误如何解决(2360)
- 9.Js异步async、await关键字详细介绍(lambda表达式中使用async和await关键字)(2236)
- 10.HBuilder编辑器格式化代码(2148)