如何使用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");
}
}
