我有以下 DTO,为此我制定了一些验证规则:
[Route("/warranties/{Id}", "GET, PUT, DELETE")]
[Route("/warranties", "POST")]
public class WarrantyDto : IReturn<WarrantyDto>
{
public int Id { get; set; }
public int StatusId { get; set; }
public string AccountNumber { get; set; }
}
验证规则:
public class WarrantyDtoValidator : AbstractValidator<WarrantyDto>
{
public WarrantyDtoValidator()
{
RuleSet(ApplyTo.Post, () => RuleFor(x => x.AccountNumber).NotEmpty());
RuleSet(ApplyTo.Put, () =>
{
RuleFor(x => x.Id).NotEmpty();
RuleFor(x => x.AccountNumber).NotEmpty();
});
RuleSet(ApplyTo.Delete, () => RuleFor(x => x.Id).NotEmpty());
}
}
在 AppHost 中设置验证:
Plugins.Add(new ValidationFeature());
container.RegisterValidators(typeof (WarrantyDtoValidator).Assembly);
FluentValidationModelValidatorProvider.Configure(provider =>
{
provider.ValidatorFactory = new FunqValidatorFactory(container);
});
然后,当我发布 WarrantyDto 时,如果我不输入以下内容,验证似乎不起作用AccountNumber
:
[POST("create")]
public ActionResult Create(WarrantyDto model)
{
if (!ModelState.IsValid) return View(model);
_warrantyService.Post(model);
return RedirectToAction("Index");
}
它似乎只是在_warrantyService.Post(model);
没有尝试首先验证的情况下击中,有什么想法吗?