Introduction
When building an ASP.NET Core Web API, one of the most important things is making sure that the data coming into your API is correct and safe. If invalid or incomplete data reaches your business logic, it can cause bugs, unexpected behavior, or even security issues.
This is where request validation becomes important. Instead of writing validation code again and again inside every controller, ASP.NET Core provides a clean solution using filters.
A request validation filter allows you to validate incoming API requests before they reach your controller logic. This helps you build clean, scalable, and production-ready Web APIs.
In this article, you will learn step by step how to implement a request validation filter in ASP.NET Core Web API using simple words, real examples, and best practices.
What is a Request Validation Filter?
A request validation filter is a custom component in ASP.NET Core that runs before your API action method executes.
In simple terms, it checks the request data first. If the data is valid, the request continues to the controller. If not, it stops the request and returns an error response.
This approach is very useful because:
You don’t need to repeat validation logic in multiple controllers
Your controller code stays clean and focused on business logic
Validation becomes reusable and easy to manage
For example, if your API expects a user object, the filter can check if the name, email, and age are valid before processing the request.
Understanding Filters in ASP.NET Core Web API
ASP.NET Core provides different types of filters that run at different stages of the request pipeline.
Here are the main types in simple words:
Authorization Filters → Used for checking authentication and permissions
Resource Filters → Run before and after request processing
Action Filters → Run before and after controller actions (most useful for validation)
Exception Filters → Handle errors globally
Result Filters → Run before and after sending the response
For request validation in ASP.NET Core Web API, we mainly use Action Filters because they run just before the controller action executes.
Step 1: Create a Request Model
First, we create a model that represents the data coming from the client.
public class UserRequest
{
public string Name { get; set; }
public string Email { get; set; }
public int Age { get; set; }
}
This model represents the request body of your API. Now we will validate this data using a custom filter.
Step 2: Create a Custom Request Validation Filter
Now we create a custom validation filter using IActionFilter.
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
public class ValidateUserRequestFilter : IActionFilter
{
public void OnActionExecuting(ActionExecutingContext context)
{
if (!context.ActionArguments.ContainsKey("user"))
{
context.Result = new BadRequestObjectResult(new
{
Message = "Request body is missing",
Status = 400
});
return;
}
var user = context.ActionArguments["user"] as UserRequest;
if (user == null)
{
context.Result = new BadRequestObjectResult(new
{
Message = "Invalid request format",
Status = 400
});
return;
}
if (string.IsNullOrWhiteSpace(user.Name))
{
context.Result = new BadRequestObjectResult(new
{
Message = "Name is required",
Status = 400
});
return;
}
if (string.IsNullOrWhiteSpace(user.Email))
{
context.Result = new BadRequestObjectResult(new
{
Message = "Email is required",
Status = 400
});
return;
}
if (user.Age <= 0)
{
context.Result = new BadRequestObjectResult(new
{
Message = "Age must be greater than 0",
Status = 400
});
return;
}
}
public void OnActionExecuted(ActionExecutedContext context)
{
// No logic required after execution
}
}
In simple words, this filter checks:
Whether the request body exists
Whether the object is valid
Whether required fields are filled
Whether values are correct
If any condition fails, it immediately returns a BadRequest response.
Step 3: Register the Filter in Dependency Injection
Now we need to register this filter so ASP.NET Core can use it.
builder.Services.AddScoped<ValidateUserRequestFilter>();
Join the conversation! Your thoughts help the community grow.