ASP.NET WEB API 自定义模型校验过滤器
2017-07-03 14:37
260 查看
对外公开WEB接口时,对模型校验是常见的安全常识,常见的写法是在controller中判断ModelState.IsValid,以注册用户API为例。
Model:
打印校验失败的错误消息代码:
Controller:
以上写法是在controller里进行校验,缺点是每个需要进行校验的controller都要写一次,为了消除重复,可以将校验代码写入全局过滤器中,由过滤器进行统一模型校验,修改后的代码:
然后在全局过滤器(WebApiConfig)注册一下: config.Filters.Add(new ValidationModelFilter());
Model:
public class RegisterCustomerModel { [Required(ErrorMessage = "姓名不能为空")] [StringLength(10,ErrorMessage = "姓名长度不能超过10个字")] public string Name { get; set; } [Required(ErrorMessage = "电话不能为空")] [RegularExpression(@"^1[34578]\d{9}$", ErrorMessage = "电话号码格式不正确")] public string Phone { get; set; } [Required(ErrorMessage = "密码不能为空")] [StringLength(48, ErrorMessage = "密码长度不能超过48个字符")] public string Password { get; set; } }
打印校验失败的错误消息代码:
public static class ModelStateExtension { public static string ToErrorMessage(this ModelStateDictionary modelStateDictionary) { var stringBuilder = new StringBuilder(); foreach (var value in modelStateDictionary.Values) { foreach (var error in value.Errors) { stringBuilder.AppendLine(error.ErrorMessage); } } return stringBuilder.ToString(); } }
Controller:
public ResponseProtocol Register(RegisterCustomerModel registerCustomerModel) { if (!ModelState.IsValid) { return new ResponseProtocol((int)ResponseResultEnum.ValidateError, ModelState.ToErrorMessage(), string.Empty); } Customer customer = new Customer { Name = registerCustomerModel.Name, Phone = registerCustomerModel.Phone, WeiXinNo = registerCustomerModel.WeiXinNo, Company = registerCustomerModel.Company, UpdateTime = DateTime.Now }; _customerService.Add(customer); return new ResponseProtocol((int)ResponseResultEnum.Success, "注册成功", string.Empty); }
以上写法是在controller里进行校验,缺点是每个需要进行校验的controller都要写一次,为了消除重复,可以将校验代码写入全局过滤器中,由过滤器进行统一模型校验,修改后的代码:
public class ValidationModelFilter : ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext actionContext) { //get方法不进行模型校验 if (actionContext.Request.Method.Method=="GET") { return; } if (!actionContext.ModelState.IsValid) { var error = JsonConvert.SerializeObject(new ResponseProtocol() { Code =(int)ResponseResultEnum.ValidateError, Message = actionContext.ModelState.ToErrorMessage(), Data = string.Empty }); var httpResponseMessage = new HttpResponseMessage { Content = new StringContent(error) }; httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); actionContext.Response = httpResponseMessage; } } }
然后在全局过滤器(WebApiConfig)注册一下: config.Filters.Add(new ValidationModelFilter());
相关文章推荐
- Asp.net Web Api开发(第三篇)自定义异常过滤器
- 使用ASP.NET WEB API构建基于REST风格的服务实战系列教程(一)——使用EF6构建数据库及模型
- 【ASP.NET Web API教程】2.3.2 创建域模型
- ASP.NET Web API 管道模型
- Asp.Net页面模型---自定义处理程序
- 使用ASP.NET WEB API构建基于REST风格的服务实战系列教程(一)——使用EF6构建数据库及模型
- asp.net Mvc 的自定义模型绑定
- asp.net MVC之 自定义过滤器(Filter)
- 【ASP.NET Web API教程】2.3.2 创建域模型
- ASP.Net MVC开发基础学习笔记:四、校验、AJAX与过滤器
- 启用和自定义 ASP.NET Web API 服务的安全性
- ASP.NET Web API教程 创建域模型的方法详细介绍
- 一套基于asp.net的安全校验机制应用模型 !
- 【ASP.NET Web API教程】6.4 模型验证
- Asp.Net Web API 2第十五课——Model Validation(模型验证)
- 【ASP.NET Web API教程】6.4 模型验证
- Asp.Net Web API 2第十五课——Model Validation(模型验证)
- ASP.NET MVC 扩展之自定义模型验证,客户端 + 服务器端
- 【ASP.NET Web API教程】6 格式化与模型绑定
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【四】——实现模型工厂,依赖注入以及格式配置