@@ -17,34 +17,24 @@ public class ResultHandlerAttribute : ResultFilterAttribute
1717 /// <param name="context">结果执行上下文</param>
1818 public override void OnResultExecuting ( ResultExecutingContext context )
1919 {
20- // 控制器过滤
21- if ( context . Controller . GetType ( ) . GetCustomAttributes < IgnoreResultHandlerAttribute > ( ) . Any ( ) )
22- {
20+ if ( HasIgnoreResultHandler ( context ) )
21+ return ;
22+ if ( HasIgnoreHandle ( context . Result ) )
2323 return ;
24- }
25- // Action过滤
26- if ( context . ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor )
27- {
28- var ignore = controllerActionDescriptor . MethodInfo
29- . GetCustomAttributes < IgnoreResultHandlerAttribute > ( ) . Any ( ) ;
30- if ( ignore )
31- return ;
32- }
3324
3425 if ( context . Result is ValidationFailedResult validationFailedResult )
3526 {
36- context . Result = new JsonResult ( new
27+ context . Result = new JsonResult ( new
3728 {
3829 Code = ( int ) StatusCode . Fail ,
39- Message = validationFailedResult . AllowMultipleResult ? validationFailedResult . Errors . FirstOrDefault ( ) ? . Message : "验证数据失败!" ,
30+ Message = validationFailedResult . AllowMultipleResult
31+ ? validationFailedResult . Errors . FirstOrDefault ( ) ? . Message
32+ : "验证数据失败!" ,
4033 Errors = validationFailedResult . Errors
4134 } ) ;
4235 return ;
4336 }
4437
45- if ( context . Result is ApiResult )
46- return ;
47-
4838 if ( context . Result is BadRequestObjectResult badRequestObjectResult )
4939 {
5040 if ( badRequestObjectResult . Value is ValidationProblemDetails details )
@@ -68,5 +58,35 @@ public override void OnResultExecuting(ResultExecutingContext context)
6858 context . Result = new ApiResult ( StatusCode . Ok , string . Empty , jsonResult . Value ) ;
6959 }
7060 }
61+
62+ /// <summary>
63+ /// 是否忽略结果处理
64+ /// </summary>
65+ /// <param name="context">结果执行上下文</param>
66+ private bool HasIgnoreResultHandler ( ResultExecutingContext context )
67+ {
68+ // 控制器过滤
69+ if ( context . Controller . GetType ( ) . GetCustomAttributes < IgnoreResultHandlerAttribute > ( ) . Any ( ) )
70+ return true ;
71+ // Action过滤
72+ if ( context . ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor )
73+ {
74+ var ignore = controllerActionDescriptor . MethodInfo . GetCustomAttributes < IgnoreResultHandlerAttribute > ( ) . Any ( ) ;
75+ if ( ignore )
76+ return true ;
77+ }
78+ return false ;
79+ }
80+
81+ /// <summary>
82+ /// 是否忽略处理
83+ /// </summary>
84+ /// <param name="result">操作结果</param>
85+ private bool HasIgnoreHandle ( IActionResult result )
86+ {
87+ if ( result is ApiResult )
88+ return true ;
89+ return false ;
90+ }
7191 }
7292}
0 commit comments