Listen to this Post
This repo demonstrates support for System.ComponentModel.DataAnnotations-based validations in minimal APIs 🏁:
GitHub – captainsafia/minapi-validation-support
You Should Know:
- Setting Up Minimal API Validation in .NET 10
To enable data annotations in Minimal APIs, ensure you have .NET 10 Preview 3 (or later) installed.
Installation & Setup:
dotnet new webapi -minimal -n MinApiValidationDemo cd MinApiValidationDemo dotnet add package Microsoft.AspNetCore.Mvc.DataAnnotations
2. Implementing Validation in Minimal APIs
Here’s a sample endpoint with validation:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddValidatorsFromAssemblyContaining<Program>();
var app = builder.Build();
app.MapPost("/users", (User user) =>
{
return Results.Ok($"User {user.Name} created!");
});
app.Run();
public class User
{
[Required(ErrorMessage = "Name is required")]
[StringLength(50, MinimumLength = 3)]
public string Name { get; set; }
[EmailAddress(ErrorMessage = "Invalid email format")]
public string Email { get; set; }
}
3. Testing Validation with cURL
curl -X POST -H "Content-Type: application/json" -d '{"Name":"", "Email":"invalid"}' http://localhost:5000/users
Expected Output:
{
"type": "https://tools.ietf.org/html/rfc7231section-6.5.1",
"title": "Bad Request",
"status": 400,
"errors": {
"Name": ["Name is required"],
"Email": ["Invalid email format"]
}
}
4. Custom Validators
Extend validation with custom attributes:
public class AgeValidationAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext context)
{
if ((int)value < 18)
return new ValidationResult("Must be 18 or older");
return ValidationResult.Success;
}
}
- Linux & Windows Commands for .NET Devs
| Command | Description |
||-|
| `dotnet watch run` | Hot-reloads .NET app |
| `dotnet ef migrations add Initial` | Creates DB migration |
| `dotnet user-secrets set “Key” “Value”` | Stores secrets locally |
| `sudo lsof -i :5000` (Linux) | Checks port usage |
| `netstat -ano | findstr :5000` (Windows) | Finds PID using port |
What Undercode Say:
Minimal APIs in .NET 10 bring simplicity without sacrificing validation power. The integration of `DataAnnotations` reduces boilerplate while maintaining robustness. For Linux-based deployments, ensure:
sudo systemctl enable dotnet-service sudo journalctl -u dotnet-service -f Monitor logs
Windows admins can automate deployments via:
New-WebAppPool -Name "MinApiAppPool" Publish-WebApplication -Configuration Release
Expected Output:
A streamlined Minimal API with built-in validation, deployable across Linux/Windows with minimal overhead.
🔗 Reference: .NET 10 Preview 3 Release Notes
References:
Reported By: Davidcallan Github – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅



