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

 

一、安装:

ASP NET Core ---FluentValidation

 

 二、应用:

         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

 

相关文章:

  • 2021-08-03
  • 2022-02-19
  • 2022-02-15
  • 2021-10-23
  • 2022-12-23
  • 2022-12-23
  • 2021-02-19
猜你喜欢
  • 2020-11-12
  • 2019-06-12
  • 2022-02-23
  • 2021-08-06
  • 2022-12-23
  • 2022-12-23
  • 2022-02-24
相关资源
相似解决方案