ASP.NET Web API 数据验证
第一种方式单独为每一个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 数据验证的更多相关文章
- ASP.NET Web API模型验证以及异常处理方式
ASP.NET Web API的模型验证与ASP.NET MVC一样,都使用System.ComponentModel.DataAnnotations. 具体来说,比如有:[Required(Erro ...
- ASP.NET Web API身份验证和授权
英语原文地址:http://www.asp.net/web-api/overview/security/authentication-and-authorization-in-aspnet-web-a ...
- ASP.NET Web API 安全验证之摘要(Digest)认证
在基本认证的方式中,主要的安全问题来自于用户信息的明文传输,而在摘要认证中,主要通过一些手段避免了此问题,大大增加了安全性. 1.客户端匿名的方式请求 (无认证) HTTP/ Unauthorized ...
- asp.net Web API 身份验证 不记名令牌验证 Bearer Token Authentication 简单实现
1. Startup.Auth.cs文件 添加属性 1 public static OAuthBearerAuthenticationOptions OAuthBearerOptions { get; ...
- asp.net web api 权限验证的方法
思路:客户端使用header或者form讲验证信息传入api,在权限验证过滤中进行处理,代码示例: 定义过滤器 public class ApiFilter1 : System.Web.Http.Au ...
- ASP.NET Web API 数据提供系统相关类型及其关系
- 【转】ASP.NET WEB API系列教程
from: 西瓜小强 http://www.cnblogs.com/risk/category/406988.html ASP.NET Web API教程(六) 安全与身份认证 摘要: 在实际的项目应 ...
- ASP.NET web api 跨域请求
1.学习文章:AJAX 跨域请求 - JSONP获取JSON数据 1.asp.net代码 参考文章:http://www.sxt.cn/info-2790-u-756.html (1).增加CorsH ...
- ASP.NET Web API 安全筛选器
原文:https://msdn.microsoft.com/zh-cn/magazine/dn781361.aspx 身份验证和授权是应用程序安全的基础.身份验证通过验证提供的凭据来确定用户身份,而授 ...
随机推荐
- BZOJ 3907: 网格
Description 求不跨过直线 \(y=x\) ,到达 \((n,m)\) 的方案数. Sol 组合数学+高精度. 这个推导过程跟 \(Catalan\) 数是一样的. 答案就是 \(C^{n+ ...
- phpDocumentor 注释语法详解
PHPDocumentor是强大的代码注释生成器,本文对各个参数进行了简单地的总结: @abstract-------------使用@abstract标记来声明一个方法,类变量或类必须重新定义子类中 ...
- 解压.tar.gz出错gzip: stdin: not in gzip format tar: /Child returned status 1 tar: Error is not recoverable: exiting now
先查看文件真正的属性是什么? [root@xxxxx ~]# tar -zxvf tcl8.4.16-src.tar.gz gzip: stdin: not in gzip format tar: ...
- centos 终端 字体颜色
默认情况下,没有颜色. https://www.centos.org/docs/2/rhl-gsg-en-7.2/ls-color.html git也默认没有颜色,破解如下: git config - ...
- CTSC2016游记
打了几天酱油.. day1 3分滚..考场上打了5+0+3,5文件名挂了. (因为5那题我会nlog^3n做法,然而只是暴力分而已.(被KDTree艹过去的一题)) 提答xjb玩了三分,原因是exgc ...
- IDEA集成MAVEN 报错
解决-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment varia ...
- js判断checkbox状态,处理表单提交事件
功能描述:手机网页需要一个投票功能,通过form的post提交.有5-20个checkbox选项,至少选一项,至多选三项.需要在用户点击提交按钮前,判断checkbox的状态是否符合条件,符合则提交到 ...
- HDFS原理介绍
HDFS(Hadoop Distributed File System )Hadoop分布式文件系统.是根据google发表的论文翻版的.论文为GFS(Google File System)Googl ...
- jq隐藏页面的一行
<script type="text/javascript" src="http://files.cnblogs.com/914556495wxkj/jquery- ...
- python to be Windows Daemon
参考:http://assback.iteye.com/blog/1731565 安装 pywin32-.win32-py2..exe #32bit pywin32-.win-amd64-py2..e ...