asp.net core ModelState 模型状态验证扩展类

  1 using DMS.Common.BaseResult;
  2 using Microsoft.AspNetCore.Mvc.ModelBinding;
  3 using System;
  4 using System.Collections.Generic;
  5 using System.Linq;
  6 using System.Text;
  7 using System.Threading.Tasks;
  8 
  9 /// <summary>
 10 /// 聚合扩展方法类
 11 /// </summary>
 12 public static class ModelStateExts
 13 {
 14     #region [ 得到模型错误消息 NonAction Task<string> ModelStateMsg(ModelStateDictionary modelState, List<string> PartValidKeys = null) ]
 15 
 16     /// <summary>
 17     /// 得到模型错误消息
 18     /// <code>
 19     /// <![CDATA[
 20     ///     if (ModelState.IsValidOK(out string errmsg))
 21     ///     {
 22     ///         return await Service.GetLiveVodAndVideoProductList(CurrentUserTicket.ID.ToLong(), param);
 23     ///     }
 24     ///     else
 25     ///     {
 26     ///         return new ResponsePageResult<ProductVodOrVideoResult> { errno = 1, errmsg = errmsg };
 27     ///     }
 28     /// ]]>
 29     /// </code>
 30     /// </summary>
 31     /// <param name="modelState"></param>
 32     /// <param name="errmsg">返回的错误消息</param>
 33     /// <param name="PartValidKeys">要验证的属性集合</param>
 34     /// <returns></returns>
 35     public static bool IsValidOK(this ModelStateDictionary modelState, out string errmsg, List<string> PartValidKeys = null)
 36     #region [ MyRegion ]
 37     {
 38         var errorCount = 0;
 39 
 40         List<string> sb = new List<string>();
 41         //获取所有错误的Key
 42         List<string> keys = PartValidKeys ?? modelState.Keys.ToList();
 43 
 44         //获取每一个key对应的ModelStateDictionary
 45         foreach (var key in keys)
 46         {
 47             var errors = modelState[key].Errors.ToList();
 48             errorCount += errors.Count;
 49 
 50             //将错误描述添加到sb中
 51             foreach (var error in errors)
 52             {
 53                 sb.Add(error.ErrorMessage);
 54             }
 55         }
 56 
 57         var result = string.Join("", sb);
 58         if (string.IsNullOrWhiteSpace(result))
 59         {
 60             result = $"{result}。";
 61         }
 62 
 63         errmsg = result;
 64 
 65         return errorCount > 0 ? false : true;
 66     }
 67     #endregion
 68 
 69     #endregion
 70 
 71     #region [ 进行模型验证,并返回服务结果 ]
 72 
 73     /// <summary>
 74     /// 执行模型验证,并返回指定服务方法的执行结果
 75     /// 调用方式:return await ModelState.GetResult(Service.ActionName, param);
 76     /// </summary>
 77     /// <typeparam name="TParam"></typeparam>
 78     /// <typeparam name="TResult"></typeparam>
 79     /// <param name="modelState"></param>
 80     /// <param name="func"></param>
 81     /// <param name="param"></param>
 82     /// <param name="PartValidKeys"></param>
 83     /// <returns></returns>
 84     public async static Task<TResult> Execute<TParam, TResult>(this ModelStateDictionary modelState, Func<TParam, Task<TResult>> func, TParam param, List<string> PartValidKeys = null) where TResult : class, new()
 85         #region [ MyRegion ]
 86     {
 87         if (IsValidOK(modelState, out string errmsg, PartValidKeys: PartValidKeys))
 88         {
 89             var result = new TResult { };
 90 
 91             return await func.Invoke(param);
 92         }
 93         else
 94         {
 95             return new DataResultBase { errno = 600, errmsg = errmsg } as TResult;
 96         }
 97     }
 98     #endregion
 99 
100     #endregion
101 }
原文地址:https://www.cnblogs.com/webenh/p/11988778.html