其他分享
首页 > 其他分享> > ActionFilterAttribute

ActionFilterAttribute

作者:互联网

1、新建类CustomActionResultFilterAttribute,继承ActionFilterAttribute

 1 using Microsoft.AspNetCore.Mvc;
 2 using Microsoft.AspNetCore.Mvc.Filters;
 3 using Project6.Model;
 4 
 5 namespace Project6.Utility.Filters
 6 {
 7     public class CustomActionResultFilterAttribute:ActionFilterAttribute
 8     {
 9         private readonly ILogger<CustomActionResultFilterAttribute> _ILogger;
10 
11         public CustomActionResultFilterAttribute(ILogger<CustomActionResultFilterAttribute> iLogger)
12         {
13             this._ILogger = iLogger;
14         }
15 
16         public override void OnActionExecuting(ActionExecutingContext context)
17         {
18             var para = context.HttpContext.Request.QueryString.Value;
19             var controllerName = context.HttpContext.GetRouteValue("controller");
20             var actionName = context.HttpContext.GetRouteValue("action");
21             _ILogger.LogInformation($"执行{controllerName} - {actionName},参数为:{para}");
22         }
23 
24         public override void OnActionExecuted(ActionExecutedContext context)
25         {
26             var result = Newtonsoft.Json.JsonConvert.SerializeObject(context.Result);
27             var controllerName = context.HttpContext.GetRouteValue("controller");
28             var actionName = context.HttpContext.GetRouteValue("action");
29             _ILogger.LogInformation($"执行{controllerName} - {actionName},结果为:{result}");
30         }
31 
32         public  override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
33         {
34             var controllerName = context.HttpContext.GetRouteValue("controller");
35             var actionName = context.HttpContext.GetRouteValue("action");
36             var para = context.HttpContext.Request.QueryString.Value;
37             _ILogger.LogInformation($"执行{controllerName} - {actionName}方法:参数为:{para}");
38 
39             //去执行控器里面的Action
40             ActionExecutedContext executedContext = await next.Invoke();
41 
42             var result = Newtonsoft.Json.JsonConvert.SerializeObject(executedContext.Result);
43             _ILogger.LogInformation($"执行{controllerName} - {actionName}方法:结果为:{result}");
44         }
45 
46         public override void OnResultExecuting(ResultExecutingContext context)
47         {
48             if (context.Result is JsonResult)
49             {
50                 JsonResult jsonResult = (JsonResult)context.Result;
51                 context.Result = new JsonResult(new AjaxResult()
52                 {
53                     Success = true,
54                     Message = "OK",
55                     Data = jsonResult.Value
56                 });
57             }
58         }
59 
60         public override void OnResultExecuted(ResultExecutedContext context)
61         {
62             base.OnResultExecuted(context);
63         }
64 
65         public override async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
66         {
67             if (context.Result is JsonResult)
68             {
69                 JsonResult jsonResult = (JsonResult)context.Result;
70                 context.Result = new JsonResult(new AjaxResult()
71                 {
72                     Success = true,
73                     Message = "OK",
74                     Data = jsonResult.Value
75                 });
76             }
77 
78             await next.Invoke();
79         }
80     }
81 }
View Code

2、新建类AjaxResult

 1 namespace Project6.Model
 2 {
 3     public class AjaxResult
 4     {
 5         public bool Success { get; set; }
 6         public string Message { get; set; }
 7         public object Data { get; set; }
 8 
 9     }
10 }
View Code

3、新建控制器CustomActionResultFilterController

 1 using Microsoft.AspNetCore.Mvc;
 2 using Project6.Utility.Filters;
 3 
 4 namespace Project6.Controllers
 5 {
 6     public class CustomActionResultFilterController : Controller
 7     {
 8         [TypeFilter(typeof(CustomActionResultFilterAttribute))]
 9         public IActionResult Index(int id)
10         {
11             int _id = id;
12             return Json(new
13             {
14                 Id = _id,
15                 Name = "net"
16             });
17         }
18     }
19 }
View Code

4、在各个方法开始处设置断点,执行/CustomActionResultFilter?id=1

可以发现程序执行顺序为:OnActionExecutionAsync--->

执行到 ActionExecutedContext executedContext = await next.Invoke();时跳到控制器CustomActionResultFilterController的Index-->

执行为Action方法之后,继续执行 ActionExecutedContext executedContext = await next.Invoke();之后的代码-->

OnResultExecutionAsync

结论:1、ActionFilterAttribute抽像类继承 Attribute, IActionFilter, IFilterMetadata, IAsyncActionFilter, IResultFilter, IAsyncResultFilter, IOrderedFilter

 

 只要继承它即可以实现常见的过滤方法。

2、当同步方法和异步方法同时存在,程序只处理异步方法

标签:JsonResult,Result,context,var,ActionFilterAttribute,HttpContext,public
来源: https://www.cnblogs.com/handsomeziff/p/16220366.html