Model validation is a fundamental aspect of building secure, reliable, and maintainable ASP.NET Core Web API applications. Proper validation ensures that incoming client requests contain correct, safe, and expected data before business logic is executed. While .NET provides built-in validation using Data Annotations, FluentValidation offers a more expressive, flexible, and scalable approach for validating request models in modern enterprise applications.
This article explains how to validate request models using FluentValidation in .NET, including internal concepts, practical implementation, real-world scenarios, architectural considerations, advantages, disadvantages, and best practices.
What Is FluentValidation?
FluentValidation is a popular validation library for .NET that allows developers to define strongly-typed validation rules using a fluent API. Instead of decorating models with attributes, validation logic is placed in separate validator classes.
In simple terms, FluentValidation keeps validation rules clean, readable, and separated from domain models.
Why Not Rely Only on Data Annotations?
ASP.NET Core provides validation using attributes such as:
[Required]
[StringLength]
[Range]
[EmailAddress]
While useful for simple scenarios, Data Annotations have limitations:
Validation logic is mixed with model classes.
Complex conditional rules become messy.
Reusability is limited.
Harder to test independently.
FluentValidation solves these problems by moving validation logic into dedicated classes.
Real-World Analogy
Think of Data Annotations as writing rules directly on a form. FluentValidation is like having a separate rulebook that inspectors follow before approving the form.
This separation improves clarity and maintainability.
Step 1: Install FluentValidation
Install the NuGet package:
dotnet add package FluentValidation.AspNetCore
Step 2: Create a Request Model
Example request model for user registration:
public class RegisterUserRequest
{
public string FullName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public int Age { get; set; }
}
Step 3: Create a Validator Class
Create a separate validator:
using FluentValidation;
public class RegisterUserRequestValidator : AbstractValidator<RegisterUserRequest>
{
public RegisterUserRequestValidator()
{
RuleFor(x => x.FullName)
.NotEmpty().WithMessage("Full name is required.")
.MinimumLength(3);
RuleFor(x => x.Email)
.NotEmpty()
.EmailAddress();
RuleFor(x => x.Password)
.NotEmpty()
.MinimumLength(8);
RuleFor(x => x.Age)
.InclusiveBetween(18, 60);
}
}
This validator defines all rules in a clean and expressive manner.
Step 4: Register FluentValidation in Program.cs
builder.Services.AddControllers();
builder.Services.AddFluentValidationAutoValidation();
builder.Services.AddValidatorsFromAssemblyContaining<RegisterUserRequestValidator>();
Now validation automatically runs before controller execution.
Step 5: Use in Controller
[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
[HttpPost]
public IActionResult Register(RegisterUserRequest request)
{
return Ok("User registered successfully.");
}
}
If validation fails, ASP.NET Core automatically returns HTTP 400 with validation errors.
Real Business Scenario: E-Commerce Checkout
In an e-commerce application:
Customer submits shipping details.
Payment information must follow strict validation rules.
Address validation may depend on country.
FluentValidation allows conditional rules:
RuleFor(x => x.Password)
.Matches("[A-Z]").WithMessage("Password must contain uppercase letter.");
RuleFor(x => x.Email)
.Must(BeCompanyEmail)
.When(x => x.Age > 25);
This flexibility is difficult with Data Annotations.
Advanced Validation: Custom Rule Example
private bool BeCompanyEmail(string email)
{
return email.EndsWith("@company.com");
}
This supports business-specific rules cleanly.
Join the conversation! Your thoughts help the community grow.