1. Create Class.
  2. Inherit from ControllerBase.
  3. Add attribute [ApiController] on class.
  4. Add action method.
  5. Give return type to method.
  6. Add Request Dto parameter in method.
  7. Add FluentValidation custom validator for Dto.
  8. Register Fluent Validation with ASP.NET.
  9. Validation done.

ASP.NET automatically adds the FluentValidation in ModelValidation pipeline. After model binding in Api Controller, framework automatically resolve validator and add Model State, that’s how automatically your Dto is validated.

  • Register controllers service.
  • Configure controller service (if required).
  • Add Fluent Validation service to the controller services to adds validation capabilities.
  • Add NewtonsoftJson service to the controller service – adds input output formatting capabilities.

Flow

  1. Request came to Middleware pipeline (app.).
  2. Controller is invoked, controller-level filters executed, including custom filters.
  3. Action method executed.
  4. Result is returned, and result filter executed (if any).
  5. Response sent to client.

Configure ApiBehaviorOptions in services to set custom response on fluent validation fail.