zoukankan      html  css  js  c++  java
  • webapi输入验证过滤器ValidationActionFilter

    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))
                    };
                }
    
            }
  • 相关阅读:
    HTML meta viewport属性说明(mark)
    利用css样式画各种图形初步、进阶、高级(一)
    sql where 1=1和 0=1 的作用
    java日志文件log4j.properties配置详解
    VB面向对象编程
    怎么上传代码?
    Spring中部署Activiti流程定义的三种姿势
    不会使用Spring的配置文件,赶紧把这个甩给他
    一起玩转玩转LiteOS组件:Opus
    带你读AI论文丨RAID2020 Cyber Threat Intelligence Modeling GCN
  • 原文地址:https://www.cnblogs.com/shiningrise/p/5695693.html
Copyright © 2011-2022 走看看