https://github.com/fkucukkara/modelvalidationworkshop
This document highlights the key differences in model validation between Controller-Based APIs and Minimal APIs, focusing on how ModelState behaves.
https://github.com/fkucukkara/modelvalidationworkshop
csharp model-state netcore-webapi validation
Last synced: about 1 year ago
JSON representation
This document highlights the key differences in model validation between Controller-Based APIs and Minimal APIs, focusing on how ModelState behaves.
- Host: GitHub
- URL: https://github.com/fkucukkara/modelvalidationworkshop
- Owner: fkucukkara
- License: mit
- Created: 2024-12-25T15:43:32.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-01-12T13:43:26.000Z (about 1 year ago)
- Last Synced: 2025-01-23T02:17:02.176Z (about 1 year ago)
- Topics: csharp, model-state, netcore-webapi, validation
- Language: C#
- Homepage:
- Size: 6.84 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Model Validation: Controller-Based API vs Minimal API
This document highlights the key differences in **model validation** between Controller-Based APIs and Minimal APIs, focusing on how **ModelState** behaves.
## Sample Model
```csharp
public class Employee
{
[Required(ErrorMessage = "ID is required.")]
[Range(1, int.MaxValue, ErrorMessage = "ID must be greater than 0.")]
public int Id { get; set; }
[Required(ErrorMessage = "Name is required.")]
public string? Name { get; set; }
}
```
## Controller-Based API
In a Controller-Based API, **ModelState** is available and validation is automatic when using the `[ApiController]` attribute.
### Example:
```csharp
[ApiController]
[Route("api/[controller]")]
public class EmployeeController : ControllerBase
{
[HttpPost]
public IActionResult AddEmployee(Employee employee)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState); // Automatically populated.
}
return Ok(employee);
}
}
```
## Minimal API
In Minimal APIs, **ModelState is not available**. Validation must be performed manually using tools like `Validator.TryValidateObject`.
### Example:
```csharp
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapPost("/employee", (Employee employee) =>
{
var validationResults = new List();
var validationContext = new ValidationContext(employee);
if (!Validator.TryValidateObject(employee, validationContext, validationResults, true))
{
return Results.BadRequest(validationResults);
}
return Results.Ok(employee);
});
app.Run();
```
## Comparison
| Feature | Controller-Based API | Minimal API |
|-----------------------------|---------------------------------------|-----------------------------------------|
| **ModelState Behavior** | Available and automatic validation | Not available; manual validation needed |
| **Ease of Use** | Easier with `[ApiController]` | Requires custom validation logic |
## Summary
- **Controller-Based API**: Leverage automatic **ModelState** validation for simplicity.
- **Minimal API**: Handle validation manually as **ModelState is not available**.