liveBook · Manning

FluentValidation.AspNetCore

As explained in ASP.NET Core in Action, 3e

FluentValidation.AspNetCore is a NuGet package that integrates FluentValidation with ASP.NET Core application validation.

FluentValidation.AspNetCore connects FluentValidation to ASP.NET Core MVC and Razor Pages validation. Install the NuGet package, call AddFluentValidationAutoValidation() in Program.cs, and register validators with dependency injection. section 32.4.2 section 32.5

A typical configuration can:

  • Discover validators automatically with AddValidatorsFromAssemblyContaining<Program>(), or register individual validators manually. section 32.4.2 section 32.5
  • Disable DataAnnotations validation to avoid running two validation systems, unless both are temporarily needed during migration. section 32.4.2 section 32.5
  • Keep validators near the models they validate; Razor Pages input-model validators can be nested inside the PageModel. section 32.4.2
Listing 32.15 Configuring FluentValidation in an ASP.NET Core application
var builder = WebApplication.CreateBuilder(args);
 
builder.Services.AddRazorPages();
 
builder.Services.AddValidatorsFromAssemblyContaining<Program>();   #1
 
builder.Services.AddFluentValidationAutoValidation(                #2
  x => x.DisableDataAnnotationsValidation = true)                  #2
    .AddFluentValidationClientsideAdapters();                      #3
 
ValidatorOptions.Global.LanguageManager.Enabled = false;           #4

A complete Program.cs configuration scans the assembly for validators, enables automatic validation, disables DataAnnotations, adds client-side adapters, and disables localized language management. section 32.4.2

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains installation, registration, automatic discovery, DataAnnotations interaction, and validator placement without detailing validation-rule implementation.
Examples
Uses Program.cs configuration, a CurrencyConverterModelValidator, and a nested Razor Pages InputModelValidator.
What it assumes
Assumes an ASP.NET Core MVC or Razor Pages application and introduces package installation, Program.cs configuration, and dependency-injection registration.
What you can do after
Operate/configure FluentValidation.AspNetCore for automatic application validation and choose between manual or assembly-based registration.
How it connects
Connects ASP.NET Core, MVC, Razor Pages, dependency injection, validators, and DataAnnotations.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.