I am trying to use Fluent Validation in a web api 2 project but I can't get the Model State to work. I downloaded this nuget package and added this line to my Application_Start
FluentValidationModelValidatorProvider.Configure(GlobalConfiguration.Configuration);
Then in my model I added
[Validator(typeof(LotValidator))]
yet my ModelState.IsValid is always valid. I don't know what I am missing or if it the fact that I have my model in a different class project(though I do reference the FluentValidation package) then my webapi project.
You might not have added ValidateModelStateFilter to the filters.
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
//Fluent Validation
config.Filters.Add(new ValidateModelStateFilter());
FluentValidationModelValidatorProvider.Configure(config);
//snip..
}
}
I have a long post on how to do everything here - http://nodogmablog.bryanhogan.net/2016/12/fluent-validation-with-web-api-2/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With