public class validationActionFilter:ActionFilterAttribute
    {
        public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            var modelState = actionContext.ModelState;
            if(!modelState.IsValid)
            {
                dynamic errors = new JObject ( );
                foreach(var key in modelState.Keys)
                {
                    var state = modelState[key];
                    if(state.Errors.Any ( ))
                    {
                        errors[key] = state.Errors.First ( ).ErrorMessage;
                    }
                }
                actionContext.Response = new HttpResponseMessage ( HttpStatusCode.BadRequest )
                {
                    Content = new StringContent ( Convert.ToString ( errors ) )
                };
            }

        }

    }

 

public class ValidationActionFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            var modelState = actionContext.ModelState;
            if (!modelState.IsValid)
            {
                ApiResult result = new ApiResult();
                result.code = ApiResultCode.fail;
                result.msg = "输入数据验证失败";
                //找到出错的字段以及出错信息
                var errorFieldsAndMsgs = modelState.Where(m => m.Value.Errors.Any())
                    .Select(x => new { x.Key, x.Value.Errors });
                result.desc = string.Join(",", errorFieldsAndMsgs.SelectMany(p => p.Errors.Select(t => t.ErrorMessage)).ToArray());

                actionContext.Response = new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content = new StringContent(JsonConvert.SerializeObject(result))
                };
            }

        }
public class ValidationActionFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            var modelState = actionContext.ModelState;
            if (!modelState.IsValid)
            {
                ApiResult result = new ApiResult();
                result.code = ApiResultCode.fail;
                result.msg = "输入数据验证失败";
                //找到出错的字段以及出错信息
                var errorFieldsAndMsgs = modelState.Where(m => m.Value.Errors.Any())
                    .Select(x => new { x.Key, x.Value.Errors });
                result.desc = string.Join(",", errorFieldsAndMsgs.SelectMany(p => p.Errors.Select(t => t.ErrorMessage)).ToArray());

                actionContext.Response = new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content = new StringContent(JsonConvert.SerializeObject(result))
                };
            }

        }

 

相关文章:

  • 2021-07-31
  • 2022-01-18
  • 2022-01-01
  • 2022-01-17
  • 2022-12-23
  • 2022-12-23
  • 2021-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-10
  • 2022-02-16
相关资源
相似解决方案