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))
                    };
                }
    
            }
  • 相关阅读:
    [UE4]创建多把枪,使用Class,参数的对象类型
    [UE4]换枪需要做的事,容器:数组、集合、Map
    [UE4]蓝图重构
    [UE4]为什么会有类型检查
    [UE4]ChildActor组件
    [UE4]Character,飞行模式。
    [UE4]Pawn和Controller,第一人称和第三人称切换
    [UE4]组件
    [UE4]Acotr
    [UE4]封装、继承、多态
  • 原文地址:https://www.cnblogs.com/shiningrise/p/5695693.html
Copyright © 2011-2022 走看看