Search code examples
asp.net-coreasp.net-core-webapifluentvalidation

How can i set cascade mode Globally?


I am new to .net core web API. I am using fluent validation API for model validation. I want to set cascade option globally for all validators. I found following line

ValidatorOptions.CascadeMode = CascadeMode.StopOnFirstFailure;  

in Given Link

But I am confused where to write this line and in which function.

Can anyone help?


Solution

  • That class is a static, so you set it as early in the pipeline as you reasonable can:

    To set the cascade mode globally, you can set the CascadeMode property on the static ValidatorOptions class during your application’s startup routine

    In a Net Core application that would be in the Startup class

    You should have something like this:

    public class Startup
    {
        // Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            ...
        }
    
        // Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app)
        {
            **ValidatorOptions.CascadeMode = CascadeMode.StopOnFirstFailure;**  
        }
    }
    

    Add it somewhere like that