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))
};
} }

webapi输入验证过滤器ValidationActionFilter的更多相关文章

  1. webapi - 模型验证

    本次要和大家分享的是webapi的模型验证,讲解的内容可能不单单是做验证,但都是围绕模型来说明的:首先来吐槽下,今天下午老板为自己买了套新办公家具,看起来挺好说明老板有钱,不好的是我们干技术的又成了搬 ...

  2. Nginx集群之SSL证书的WebApi令牌验证

    目录 1       大概思路... 1 2       Nginx集群之SSL证书的WebApi令牌验证... 1 3       Openssl生成SSL证书... 2 4       编写.NE ...

  3. Nginx集群之基于Redis的WebApi身份验证

    目录 1       大概思路... 1 2       Nginx集群之基于Redis的WebApi身份验证... 1 3       Redis数据库... 2 4       Visualbox ...

  4. ASP.NET MVC5+EF6+EasyUI 后台管理系统(66)-MVC WebApi 用户验证 (2)

    系列目录 前言: 回顾上一节,我们利用webapi简单的登录并进行了同域访问与跨域访问来获得Token,您可以跳转到上一节下载代码来一起动手. 继续上一篇的文章,我们接下来演示利用拿到的Token来访 ...

  5. ASP.NET MVC5+EF6+EasyUI 后台管理系统(65)-MVC WebApi 用户验证 (1)

    系列目录 前言: WebAPI主要开放数据给手机APP,其他需要得知数据的系统,或者软件应用,所以移动端与系统的数据源往往是相通的. Web 用户的身份验证,及页面操作权限验证是B/S系统的基础功能, ...

  6. Filter登录验证过滤器(全局)

    通过Filter来定义一个登录验证过滤器,这是就不需要在每一个JSP页面添加判断用户合法性的代码了. 以下示例中包含了5个文件,一个是登录表单LoginForm.jsp,一个是登录判断页LoginCo ...

  7. MVC WebApi 用户验证 (2)

    构建ASP.NET MVC5+EF6+EasyUI 1.4.3+Unity4.x注入的后台管理系统(66)-MVC WebApi 用户验证 (2)   前言: 构建ASP.NET MVC5+EF6+E ...

  8. Nginx集群之SSL证书的WebApi身份验证

    目录 1       大概思路... 1 2       Nginx集群之SSL证书的WebApi身份验证... 1 3       AuthorizeAttribute类... 2 4       ...

  9. WebApiClient的接口输入验证

    1. 文章目的 随着WebApiClient的不断完善,越来越多开发者选择WebApiClient替换原生的HttpClient,本文将介绍WebApiClient的接口参数输入有效性验证的新特性. ...

随机推荐

  1. zabbix的安装

    1  lamp环境搭建以及zabbix安装 方便的话使用yum方式(yum安装的是2.2版本) 安装epel环境 yum install -y epel-release 安装lamp环境 yum in ...

  2. git将本地代码 和服务器git@osc 上的代码 关联

    将本地代码 和服务器git@osc 上的代码 关联 要使用git 首先,你得安装一个git 下载 http://git-scm.com/downloads 安装完成后,需要简单的配置一下,打开 Git ...

  3. spring-从普通java类取得注入spring Ioc容器的对象的方案

    1.启动服务时通过spring容器的监听器(继承ContextLoaderListener 监听器的方法) public class ListenerSpringContext extends Con ...

  4. Leetcode 110. Balanced Binary Tree

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  5. Leetcode 313. super ugly number

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  6. 正则表达式re模块

    正则表达式模块re 1. 正则简介 就其本质而言,正则表达式(或 RE)是一种小型的.高度专业化的编程语言, (在Python中)它内嵌在Python中,并通过 re 模块实现.正则表达式模式被 编译 ...

  7. windows下安装使用Composer记录

    一直以来都是直接下载类库文件放到项目中使用 Composer是一个PHP依赖管理工具,有了它,就可以对依赖的包进行统一管理,而且在项目中可以实现自动加载 安装和使用composer 下载并且运行 Co ...

  8. HDU 1231 最大连续子序列(水题)

    题目链接: 传送门 最大连续子序列 Time Limit: 1000MS     Memory Limit: 32768 K Description 给定K个整数的序列{ N1, N2, ..., N ...

  9. 使用chrome查看网页上效果的实现方式

    使用chrome查看网页上效果的实现方式 chrome是一个极为强大的工具,很多时候,我们不知道一个效果怎么实现的,我们完全可以找到响应的网页,然后找到其html文件,和js文件,查看源码,获得其实现 ...

  10. 如何写出优雅的css代码 ?

    如何写出优雅的css代码 ? 对于同样的项目或者是一个网页,尽管最终每个前端开发工程师都可以实现相同的效果,但是他们所写的代码一定是不同的.有的优雅,看起来清晰易懂,代码具有可拓展性,这样的代码有利于 ...