ASP.NET Web API之FluentValidation验证

来源:互联网 发布:银行卡网络异地消费 编辑:程序博客网 时间:2024/06/08 05:10

最近在做Web API,用到了流式验证,就简单的说说这个流式验证。

首先我们定义一个Filter,如下

1

2

3

4

5

6

7

8

9

10

11

 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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

[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,验证不通过进行请求拦截,然后所有的APIController都继承BaseController,这样所有继承自BaseController的Controller都会走验证。

接下来我们看一下Request的定义

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

public class CustomerValidateRequest : IValidatableObject

{

    private readonly IValidator _validator;

 

    public CustomerValidateRequest()

    {

        _validator = new CustomerValidateRequestValidator();

    }

 

    public string Email { getset; }

 

    public string Password { getset; }

 

    public string ValidateCode { getset; }

 

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)

    {

        return _validator.Validate(this).ToValidationResult();

    }

}

Request定义好之后,我们在最下面写方法获取验证的结果。接下来再看看我们的Validator

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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返回的是如下的接口类型

1

public IRuleBuilderInitial<T, TProperty> RuleFor<TProperty>(Expression<Func<T, TProperty>> expression)

该接口继承IRuleBuilder接口

1

public interface IRuleBuilderInitial<T, out TProperty> : IFluentInterface, IRuleBuilder<T, TProperty>, IConfigurable<PropertyRule, IRuleBuilderInitial<T, TProperty>>

IRuleBuild有很多扩展方法在DefaultValidatorExtensions类中,如下

wKiom1dex4vgC2RPAADiaPwWg-Q515.png简直是太多了,验证信用卡,邮箱,比较大小,区域,不等于等等,当然你自己也可以扩展一些出来。

我们用Google DHC看一下效果

wKiom1deyIXRH6aMAAA64QyvxHc413.png

如果什么都不传,就会根据上面的验证规则进行验证。

wKioL1deye_wOztiAAA8hgpHD3A410.png

如果传了Email,则会验证Email是否正确。

最后记得在Globle.asax.cs中增加如下代码,注意在Nuget中找到FluentValidation和FluentValidation.MVC

1

2

3

4

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new AttributedValidatorFactory()));

ValidatorOptions.CascadeMode = CascadeMode.StopOnFirstFailure;

FluentValidationModelValidatorProvider.Configure();

原文转自:乐搏学院http://www.learnbo.com/front/article/cmsIndex


0 0
原创粉丝点击