内容目录
When not using ASP.NET Core and without the FluentValidation framework, model validation can be implemented through the native API.
public class A
{
[EmailAddress]
public string B { get; set; }
}
void Main()
{
var result = new List<ValidationResult>();
var a = new A
{
B = "aa.com"
};
var validationContext = new ValidationContext(a);
Console.WriteLine(Validator.TryValidateObject(a, validationContext, result, true));
result.All(x =>
{
Console.WriteLine($"{x.MemberNames.First()}:{x.ErrorMessage}");
return true;
}
);
}
文章评论