第一种方式单独为每一个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. java读取文件

       一个字节一个字节地读取                  File file =          InputStream inputStream =                       ...

  2. ejs模板

    nodejs的模板引擎有很多, ejs是比较简单和容易上手的.常用的一些语法: 用<%...%>包含js代码 用<%=...%>输出变量 变量若包含 '<' '>' ...

  3. jQuery常用API

    jQuery API查询网址 http://jquery.cuishifeng.cn/ Dom和jquery相互装换 jquery对象[0] => Dom对象 Dom对象 => $(Dom ...

  4. 2.3---删除链表的结点,不提供头结点(CC150)

    这里,注意如果是尾结点,那么无解. public class Solution { public void deleteNode(ListNode node) { //利用李代桃僵 // // if( ...

  5. 蓝牙模块连接后出现ANR,日志记录

    11-25 16:29:48.433 14507-14561/myapplication.com.myblue W/MALI: glDrawArrays:714: [MALI] glDrawArray ...

  6. MySQL 编程的6个重要的技巧

    一.每一行命令都是用分号(;)作为结束 对于MySQL,第一件你必须牢记的是它的每一行命令都是用分号(;)作为结束的,但当一行MySQL被插入在PHP代码中时,最好把后面的分号省略掉,例如:   二. ...

  7. Response.Redirect()、Server.Execute和Server.Transfer的区别

    1.Response.Redirect(): Response.Redirect方法导致浏览器链接到一个指定的URL. 当Response.Redirect()方法被调用时,它会创建一个应答,应答头中 ...

  8. AngularJS vs. jQuery

    很多Web开发新手都会有这样的疑问“我应该使用什么开发框架呢,如何快速学会Web开发呢?”这个问题其实没有一个统一的正确答案,其中讨论最多的就是AngularJS和jQuery的差别.这两者的之间的比 ...

  9. 小波变换C++实现(一)----单层小波变换

    文章转自: http://www.cnblogs.com/IDoIUnderstand/archive/2013/03/30/3280724.html [小波变换]STL版 一维离散小波变换(DWT) ...

  10. Codeforces 55D

    基本的数位DP,注意记录那些状态可以用最小的空间判断出整除性. #include <cstdio> #include <cstring> using namespace std ...