Fork me on GitHub

ASP NET Core ---FluentValidation

官方文档:https://fluentvalidation.net/

 

一、安装:

 

 二、应用:

         1、建立PostValidator:

   public class PostValidator:AbstractValidator<PostDTO>
    {
        public PostValidator()
        {
            RuleFor(x => x.Author).NotEmpty()
                .WithName("作者").WithMessage("{PropertyName}是必须填写的")
                .MaximumLength(50).WithMessage("{PropertyName}的最大长度是{MaxLength}");
        }
    }

          2、aspnetcore注册:

services.AddTransient<IValidator<PostDTO>, PostValidator>();

 

 

参考资料:https://v.qq.com/x/page/z0763a2x3j5.html

 

posted @ 2018-09-04 22:36  精进的小陈  阅读(913)  评论(0编辑  收藏  举报