Asp.Net Core 全局模型验证

 1 public class ActionFilter : IActionFilter
 2     {
 3 
 4         /// 
 5         /// action 执行之前
 6         /// 
 7         /// 
 8         public void OnActionExecuting(ActionExecutingContext context)
 9         {
10             #region 全局 Model 验证过滤  
11             if (context.ModelState.IsValid) return;
12             var result = new ResponseData<object>();
13             foreach (var item in context.ModelState.Values)
14             {
15                 foreach (var error in item.Errors)
16                 {
17                     result.Status = MessageInfo.StatusFailed;
18                     result.Message = error.ErrorMessage;
19                     if (string.IsNullOrEmpty(result.Message))
20                     {
21                         result.Message = "Verification is not necessary.";
22                     }
23                     context.Result = new JsonResult(result);
24                     break;
25                 }
26             }
27             #endregion
28         }
29     }

 

转载于:https://www.cnblogs.com/FanTaoLin/p/10084080.html

你可能感兴趣的:(Asp.Net Core 全局模型验证)