In ASP.NET Core-6 Web API, I am using FluentValidation.AspNetCore(11.2.1).
I have this code in the Program.cs:
builder.Services.AddMvc().AddFluentValidation(fv => {
fv.DisableDataAnnotationsValidation = true;
fv.RegisterValidatorsFromAssembly(typeof(Program).Assembly);
fv.RegisterValidatorsFromAssembly(Assembly.GetExecutingAssembly());
fv.ImplicitlyValidateChildProperties = true;
fv.ImplicitlyValidateRootCollectionElements = true;
fv.AutomaticValidationEnabled = true;
});
But I got this error with all the code above highlighted:
FluentValidationMvcExtensions.AddFluentValidation(IMvcBuilder, Action)' is obsolete: 'Calling AddFluentValidation() is deprecated
How do I get this resolved?
Thanks
As per the documentation (https://docs.fluentvalidation.net/en/latest/aspnet.html) it is not recommended to use Automatic validation.
So I will recommend to not use Automatic Validation and remove the AddMvc() because automatic validation works with AddMvc, https://github.com/FluentValidation/FluentValidation/issues/1377
Now Let's come to your problem, assuming you are using .net6, following code should work.
using FluentValidation.AspNetCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddFluentValidation(conf =>
{
conf.RegisterValidatorsFromAssembly(typeof(Program).Assembly);
conf.AutomaticValidationEnabled = false;
});