Skip to content

Commit 62b4405

Browse files
committed
Min API Quick Ref: Add validation support .NET 10 Prev4
1 parent 02f1bb1 commit 62b4405

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

aspnetcore/fundamentals/minimal-apis.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,62 @@ The <xref:System.Delegate> arguments passed to these methods are called "route h
7070

7171
[!INCLUDE [](~/fundamentals/minimal-apis/includes/parameter-binding10.md)]
7272

73+
## Validation support in Minimal APIs
74+
75+
Support for validation in Minimal APIs is now available. This feature allows you to request validation of data sent to your API endpoints. Enabling validation allows the ASP.NET Core runtime to perform any validations defined on the:
76+
77+
* Query
78+
* Header
79+
* Request body
80+
81+
Validations are defined using attributes in the [`DataAnnotations`](xref:System.ComponentModel.DataAnnotations) namespace. Developers customize the behavior of the validation system by:
82+
83+
* Creating custom [`[Validation]`](xref:System.ComponentModel.DataAnnotations.ValidationAttribute) attribute implementations.
84+
* Implementing the [`IValidatableObject`](xref:System.ComponentModel.DataAnnotations.IValidatableObject) interface for complex validation logic.
85+
86+
If validation fails, the runtime returns a 400 Bad Request response with details of the validation errors.
87+
88+
### Enable built-in validation support for minimal APIs
89+
90+
Enable the built-in validation support for minimal APIs by calling the `AddValidation` extension method to register the required services in the service container for your application:
91+
92+
```csharp
93+
builder.Services.AddValidation();
94+
```
95+
96+
The implementation automatically discovers types that are defined in minimal API handlers or as base types of types defined in minimal API handlers. An endpoint filter performs validation on these types and is added for each endpoint.
97+
98+
Validation can be disabled for specific endpoints by using the `DisableValidation` extension method, as in the following example:
99+
100+
```csharp
101+
app.MapPost("/products",
102+
([EvenNumber(ErrorMessage = "Product ID must be even")] int productId, [Required] string name)
103+
=> TypedResults.Ok(productId))
104+
.DisableValidation();
105+
```
106+
107+
### Validation with record types
108+
<!-- https://github.com/dotnet/aspnetcore/pull/61193 -->
109+
<!-- https://github.com/dotnet/aspnetcore/pull/61402 -->
110+
111+
Minimal APIs also support validation with C# record types. Record types can be validated using attributes from the <xref:System.ComponentModel.DataAnnotations?displayProperty=fullName> namespace, similar to classes. For example:
112+
113+
```csharp
114+
public record Product(
115+
[Required] string Name,
116+
[Range(1, 1000)] int Quantity);
117+
```
118+
119+
When using record types as parameters in Minimal API endpoints, validation attributes are automatically applied in the same way as class types:
120+
121+
```csharp
122+
app.MapPost("/products", (Product product) =>
123+
{
124+
// Endpoint logic here
125+
return TypedResults.Ok(product);
126+
});
127+
``
128+
73129
## Responses
74130

75131
Route handlers support the following types of return values:

0 commit comments

Comments
 (0)