第一种方式单独为每一个Action做验证

 // POST api/values
public HttpResponseMessage Post([FromBody]UserInfo userInfo)
{ if (string.IsNullOrWhiteSpace(userInfo.Gender))
{
ModelState.AddModelError("Gender", "性别不能为空");
} if (ModelState.IsValid)
{
// Do something with the product (not shown).
return new HttpResponseMessage(HttpStatusCode.OK);
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
}
public class UserInfo
{
public int Id { get; set; }
[Required]
[StringLength(, ErrorMessage = "名字太长了或者太短了", MinimumLength = )]
public string Name { get; set; } [RegularExpression(@"([2-5]\d)", ErrorMessage = "年龄在20-50之间")]
public int Age { get; set; } public string Gender { get; set; }
}

第二种做全局验证:

 public class ValidationAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (!actionContext.ModelState.IsValid)
{
actionContext.Response = actionContext.Request.CreateErrorResponse(
HttpStatusCode.BadRequest,
actionContext.ModelState);
}
}
} WebApiConfig.cs config.Filters.Add(new ValidationAttribute());

ASP.NET Web API 数据验证的更多相关文章

  1. ASP.NET Web API模型验证以及异常处理方式

    ASP.NET Web API的模型验证与ASP.NET MVC一样,都使用System.ComponentModel.DataAnnotations. 具体来说,比如有:[Required(Erro ...

  2. ASP.NET Web API身份验证和授权

    英语原文地址:http://www.asp.net/web-api/overview/security/authentication-and-authorization-in-aspnet-web-a ...

  3. ASP.NET Web API 安全验证之摘要(Digest)认证

    在基本认证的方式中,主要的安全问题来自于用户信息的明文传输,而在摘要认证中,主要通过一些手段避免了此问题,大大增加了安全性. 1.客户端匿名的方式请求 (无认证) HTTP/ Unauthorized ...

  4. asp.net Web API 身份验证 不记名令牌验证 Bearer Token Authentication 简单实现

    1. Startup.Auth.cs文件 添加属性 1 public static OAuthBearerAuthenticationOptions OAuthBearerOptions { get; ...

  5. asp.net web api 权限验证的方法

    思路:客户端使用header或者form讲验证信息传入api,在权限验证过滤中进行处理,代码示例: 定义过滤器 public class ApiFilter1 : System.Web.Http.Au ...

  6. ASP.NET Web API 数据提供系统相关类型及其关系

  7. 【转】ASP.NET WEB API系列教程

    from: 西瓜小强 http://www.cnblogs.com/risk/category/406988.html ASP.NET Web API教程(六) 安全与身份认证 摘要: 在实际的项目应 ...

  8. ASP.NET web api 跨域请求

    1.学习文章:AJAX 跨域请求 - JSONP获取JSON数据 1.asp.net代码 参考文章:http://www.sxt.cn/info-2790-u-756.html (1).增加CorsH ...

  9. ASP.NET Web API 安全筛选器

    原文:https://msdn.microsoft.com/zh-cn/magazine/dn781361.aspx 身份验证和授权是应用程序安全的基础.身份验证通过验证提供的凭据来确定用户身份,而授 ...

随机推荐

  1. qt-5.6.0 移植之tslib 配置及编译

    tslib 是qt启动时的一个触屏校正检验程序. 它的配置以及编译比较简单. 第一步, 下载tslib源码包: http://download.csdn.net/detail/MKNDG/329156 ...

  2. hibernate日常BUG总结

    在使用hibernate自动生产表的时候失败, 是配置文件我是从别地方拷贝过来忘记更改,所以报了这个错误. 重新命名了生成表的名称,问题解决! 问题很明显,自动增长的主键应该使用整型. 这里写的是St ...

  3. Google java style

    这里主要是记录关于java style的笔记. 1,Google的. 博客链接(中文):http://www.cnblogs.com/lanxuezaipiao/p/3534447.html. 官方链 ...

  4. IDEA 新建文件默认加入CVS

    是要先add,不过可以设置创建的文件都默认 add的.修改默认值看下图:打开系统设置,找到 Version Control 设置选项: 在 When files are created 选项中选择第二 ...

  5. linux下合并两个文件夹

    一.我想把自己自定义的软件统一放到man手册路径里.如何和现有的/usr/local/share文件夹合并起来,原来的文件还在? (1)下面是解压出的自定义的bashdb调试软件==>

  6. 《oracle每天一练》Oracle冷备份与数据恢复

    相关帖子 转自http://blog.csdn.net/nsj820/article/details/5611361 备份 直接拷贝oracle目录下的admin.oradata(datafile,  ...

  7. trap

    http://blog.csdn.net/elbort/article/details/8525599 http://mywiki.wooledge.org/SignalTrap

  8. oracle11g 连接问题

    一.The Network Adapter could not establish the connection  状态: 失败 -测试失败: IO 错误: The Network Adapter c ...

  9. 77 找出最大连续自然数个数[Longest Consecutive Sequence in an Unsorted Array]

    [本文链接] http://www.cnblogs.com/hellogiser/p/Longest-Consecutive-Sequence-in-an-Unsorted-Array.html [题 ...

  10. Delphi中Format与FormatDateTime函数详解

    copy:http://hi.baidu.com/yunfanleo/blog/item/0c51d9cdbc0531550eb34558.html Format是一个很常用,却又似乎很烦的方法,本人 ...