Skip to content

Commit bf67c4b

Browse files
Min API Quick Ref: Add validation support .NET 10 Prev4 (#35501)
* Min API Quick Ref: Add validation support .NET 10 Prev4 * Added missing code tick * Apply suggestions from code review Added review suggestions by mikekistler. Consolidate. Co-authored-by: Mike Kistler <[email protected]> * Added description accidently removed in review --------- Co-authored-by: Mike Kistler <[email protected]>
1 parent 59d6e15 commit bf67c4b

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

aspnetcore/fundamentals/minimal-apis.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Provides an overview of minimal APIs in ASP.NET Core
55
ms.author: wpickett
66
content_well_notification: AI-contribution
77
monikerRange: '>= aspnetcore-6.0'
8-
ms.date: 02/07/2025
8+
ms.date: 05/19/2025
99
uid: fundamentals/minimal-apis
1010
ai-usage: ai-assisted
1111
---
@@ -70,6 +70,49 @@ 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.
82+
83+
When a parameter to a Minimal API endpoint is a class or record type, validation attributes are automatically applied. For example:
84+
85+
```csharp
86+
public record Product(
87+
[Required] string Name,
88+
[Range(1, 1000)] int Quantity);
89+
```
90+
Developers customize the behavior of the validation system by:
91+
92+
* Creating custom [`[Validation]`](xref:System.ComponentModel.DataAnnotations.ValidationAttribute) attribute implementations.
93+
* Implementing the [`IValidatableObject`](xref:System.ComponentModel.DataAnnotations.IValidatableObject) interface for complex validation logic.
94+
95+
If validation fails, the runtime returns a 400 Bad Request response with details of the validation errors.
96+
97+
### Enable built-in validation support for minimal APIs
98+
99+
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:
100+
101+
```csharp
102+
builder.Services.AddValidation();
103+
```
104+
105+
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.
106+
107+
Validation can be disabled for specific endpoints by using the `DisableValidation` extension method, as in the following example:
108+
109+
```csharp
110+
app.MapPost("/products",
111+
([EvenNumber(ErrorMessage = "Product ID must be even")] int productId, [Required] string name)
112+
=> TypedResults.Ok(productId))
113+
.DisableValidation();
114+
```
115+
73116
## Responses
74117

75118
Route handlers support the following types of return values:

0 commit comments

Comments
 (0)