asp.net-core – 如何使用FluentValidation.AspNetCore和FluentValidat

如何使用FluentValidation.AspNetCore和FluentValidation.MVC6来验证AspNetCore中的实体,有谁可以举个例子?

解决方法

这对我有用:

project.json添加:

"FluentValidation.AspNetCore": "6.4.0-beta3"

startup.cs

services
.AddMvc()
.AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<Startup>());

验证:

public class Foo
{
     public string Bar {get; set;}
}

public class FooValidator : AbstractValidator<Foo> 
{
    public FooValidator()
    { 
        RuleFor(x => x.Bar).NotEmpty().WithMessage("Error Message");
    }
}

dawei

【声明】:唐山站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。