最近在做Web API,用到了流式验证,就简单的说说这个流式验证。
首先我们定义一个Filter,如下
public class ValidateResponseFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
{
if (!actionContext.ModelState.IsValid)
{
//actionContext.ModelState.Keys
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, actionContext.ModelState);
}
}
}重写Action执行方法,如果请求model存在异常,则将500error返回给客户端。
接下来我们要怎么做,定义一个BaseController
[ValidateResponseFilter]
public class BaseController : ApiController
{
protected HttpResponseMessage CreateSystemErrorResponse(string errorMsg)
{
return Request.CreateResponse<object>(
new
{
IsSuc = false,
ErrorMsg = errorMsg
});
}
protected HttpResponseMessage CreateErrorResponse(string responseCode, Type type = null, HttpStatusCode statusCode = HttpStatusCode.OK)
{
return Request.CreateResponse<object>(statusCode,
new
{
IsSuc = false,
ErrorMsg = MessageResHelper.GetMessage(type != null ? type.Name : this.GetType().Name, responseCode)
});
}
protected HttpResponseMessage CreateSucResponse(string responseCode = "")
{
if (string.IsNullOrEmpty(responseCode))
{
return Request.CreateResponse<object>(new
{
IsSuc = true
});
}
return Request.CreateResponse<object>(
new
{
IsSuc = true,
ErrorMsg = MessageResHelper.GetMessage(this.GetType().Name, responseCode)
});
}
}在BaseController上我们标记上面的Attribute,验证不通过进行请求拦截。
接下来我们看一下Request的定义
public class CustomerValidateRequest : IValidatableObject
{
private readonly IValidator _validator;
public CustomerValidateRequest()
{
_validator = new CustomerValidateRequestValidator();
}
public string Email { get; set; }
public string Password { get; set; }
public string ValidateCode { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
return _validator.Validate(this).ToValidationResult();
}
}Request定义好之后,我们在最下面写方法获取验证的结果。接下来再看看我们的Validator
public class CustomerValidateRequestValidator : AbstractValidator<CustomerValidateRequest>
{
public CustomerValidateRequestValidator()
{
RuleFor(dto => dto.Email).NotNull().NotEmpty();
RuleFor(dto => dto.Password).NotNull().NotEmpty();
RuleFor(dto => dto.ValidateCode).NotNull().NotEmpty().Length(WebAppSettings.ValidateCodeLength);
When(dto => !string.IsNullOrWhiteSpace(dto.Email), () =>
{
RuleFor(c => c.Email).Matches(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
});
}
}在这里就是我们所要验证的逻辑,可以验证最基本的非空,长度,还可以验证正则。这里RuleFor返回的是如下的接口类型
public IRuleBuilderInitial<T, TProperty> RuleFor<TProperty>(Expression<Func<T, TProperty>> expression)
该接口继承IRuleBuilder接口
public interface IRuleBuilderInitial<T, out TProperty> : IFluentInterface, IRuleBuilder<T, TProperty>, IConfigurable<PropertyRule, IRuleBuilderInitial<T, TProperty>>
IRuleBuild有很多扩展方法在DefaultValidatorExtensions类中,如下
简直是太多了,验证信用卡,邮箱,比较大小,区域,不等于等等,当然你自己也可以扩展一些出来。
我们用Google DHC看一下效果
如果什么都不传,就会根据上面的验证规则进行验证。
如果传了Email,则会验证Email是否正确。
最后记得在Globle.asax.cs中增加如下代码
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false; ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new AttributedValidatorFactory())); ValidatorOptions.CascadeMode = CascadeMode.StopOnFirstFailure; FluentValidationModelValidatorProvider.Configure();
好了,今天就到这里,更多的内容请看下面这篇博客。
https://chsakell.com/2015/01/17/web-api-powerful-custom-model-validation-with-fluentvalidation/
本文出自 “技术创造价值” 博客,请务必保留此出处http://leelei.blog.51cto.com/856755/1788944
ASP.NET Web API之FluentValidation验证
原文地址:http://leelei.blog.51cto.com/856755/1788944