编程语言
首页 > 编程语言> > asp.net core 模型验证扩展类

asp.net core 模型验证扩展类

作者:互联网

 

    /// <summary>
    /// 聚合扩展方法类
    /// </summary>
    public static class Exts
    {
        #region [ 得到模型错误消息 NonAction Task<string> ModelStateMsg(ModelStateDictionary modelState) ]
        /// <summary>
        /// 得到模型错误消息
        /// <code>
        /// <![CDATA[
        ///     if (ModelState.IsValidOK(out string errmsg))
        ///     {
        ///         return await Service.GetLiveVodAndVideoProductList(CurrentUserTicket.ID.ToLong(), param);
        ///     }
        ///     else
        ///     {
        ///         return new ResponsePageResult<ProductVodOrVideoResult> { errno = 1, errmsg = errmsg };
        ///     }
        /// ]]>
        /// </code>
        /// </summary>
        /// <param name="modelState"></param>
        /// <param name="errmsg">返回的错误消息</param>
        /// <param name="PartValidKeys">要验证的属性集合</param>
        /// <returns></returns>
        public static bool IsValidOK(this ModelStateDictionary modelState, out string errmsg, List<string> PartValidKeys = null)
        {
            var errorCount = 0;

            List<string> sb = new List<string>();
            //获取所有错误的Key
            List<string> keys = PartValidKeys ?? modelState.Keys.ToList();

            //获取每一个key对应的ModelStateDictionary
            foreach (var key in keys)
            {
                var errors = modelState[key].Errors.ToList();
                errorCount += errors.Count;

                //将错误描述添加到sb中
                foreach (var error in errors)
                {
                    sb.Add(error.ErrorMessage);
                }
            }

            var result = string.Join(",", sb);
            if (string.IsNullOrWhiteSpace(result))
            {
                result = $"{result}。";
            }

            errmsg = result;

            return errorCount > 0 ? false : true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="modelState"></param>
        /// <param name="action"></param>
        /// <param name="PartValidKeys"></param>
        /// <returns></returns>
        public static TResult GetResult<TResult>(this ModelStateDictionary modelState, Action<TResult> action, List<string> PartValidKeys = null)
            where TResult : class, new()
        {
            if (IsValidOK(modelState, out string errmsg, PartValidKeys: PartValidKeys))
            {
                var result = new TResult { };
                action(result);
                return result;
            }
            else
            {

                return new DataResultBase { errno = 600, errmsg = errmsg } as TResult;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TParam"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="modelState"></param>
        /// <param name="func"></param>
        /// <param name="param"></param>
        /// <param name="PartValidKeys"></param>
        /// <returns></returns>
        public async static Task<TResult> GetResult<TParam,TResult>(this ModelStateDictionary modelState, Func<TParam,Task<TResult>> func,TParam param, List<string> PartValidKeys = null)
            where TResult : class, new()
        {
            if (IsValidOK(modelState, out string errmsg, PartValidKeys: PartValidKeys))
            {
                var result = new TResult { };
                
                return await func.Invoke(param);
            }
            else
            {

                return new DataResultBase { errno = 600, errmsg = errmsg } as TResult;
            }
        }
        #endregion

 

标签:modelState,core,asp,PartValidKeys,TResult,result,var,net,errmsg
来源: https://www.cnblogs.com/webenh/p/11988778.html