From 62b440513fe30b0bd37a5d22f628e9aedaed5988 Mon Sep 17 00:00:00 2001 From: wadepickett Date: Mon, 19 May 2025 16:12:00 -0700 Subject: [PATCH 1/4] Min API Quick Ref: Add validation support .NET 10 Prev4 --- aspnetcore/fundamentals/minimal-apis.md | 56 +++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/aspnetcore/fundamentals/minimal-apis.md b/aspnetcore/fundamentals/minimal-apis.md index 632f7852c6ae..6fe3d67f9c89 100644 --- a/aspnetcore/fundamentals/minimal-apis.md +++ b/aspnetcore/fundamentals/minimal-apis.md @@ -70,6 +70,62 @@ The arguments passed to these methods are called "route h [!INCLUDE [](~/fundamentals/minimal-apis/includes/parameter-binding10.md)] +## Validation support in Minimal APIs + +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: + +* Query +* Header +* Request body + +Validations are defined using attributes in the [`DataAnnotations`](xref:System.ComponentModel.DataAnnotations) namespace. Developers customize the behavior of the validation system by: + +* Creating custom [`[Validation]`](xref:System.ComponentModel.DataAnnotations.ValidationAttribute) attribute implementations. +* Implementing the [`IValidatableObject`](xref:System.ComponentModel.DataAnnotations.IValidatableObject) interface for complex validation logic. + +If validation fails, the runtime returns a 400 Bad Request response with details of the validation errors. + +### Enable built-in validation support for minimal APIs + +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: + +```csharp +builder.Services.AddValidation(); +``` + +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. + +Validation can be disabled for specific endpoints by using the `DisableValidation` extension method, as in the following example: + +```csharp +app.MapPost("/products", + ([EvenNumber(ErrorMessage = "Product ID must be even")] int productId, [Required] string name) + => TypedResults.Ok(productId)) + .DisableValidation(); +``` + +### Validation with record types + + + +Minimal APIs also support validation with C# record types. Record types can be validated using attributes from the namespace, similar to classes. For example: + +```csharp +public record Product( + [Required] string Name, + [Range(1, 1000)] int Quantity); +``` + +When using record types as parameters in Minimal API endpoints, validation attributes are automatically applied in the same way as class types: + +```csharp +app.MapPost("/products", (Product product) => +{ + // Endpoint logic here + return TypedResults.Ok(product); +}); +`` + ## Responses Route handlers support the following types of return values: From 3b58a3fd359f957283d991f55f7e6ff9d4a83fe7 Mon Sep 17 00:00:00 2001 From: wadepickett Date: Mon, 19 May 2025 17:52:58 -0700 Subject: [PATCH 2/4] Added missing code tick --- aspnetcore/fundamentals/minimal-apis.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis.md b/aspnetcore/fundamentals/minimal-apis.md index 6fe3d67f9c89..3d32e6010dbd 100644 --- a/aspnetcore/fundamentals/minimal-apis.md +++ b/aspnetcore/fundamentals/minimal-apis.md @@ -5,7 +5,7 @@ description: Provides an overview of minimal APIs in ASP.NET Core ms.author: wpickett content_well_notification: AI-contribution monikerRange: '>= aspnetcore-6.0' -ms.date: 02/07/2025 +ms.date: 05/19/2025 uid: fundamentals/minimal-apis ai-usage: ai-assisted --- @@ -124,7 +124,7 @@ app.MapPost("/products", (Product product) => // Endpoint logic here return TypedResults.Ok(product); }); -`` +``` ## Responses From 76734fde7ede637a6e8140e812aa4edb6e59ddf9 Mon Sep 17 00:00:00 2001 From: Wade Pickett Date: Mon, 19 May 2025 19:09:37 -0700 Subject: [PATCH 3/4] Apply suggestions from code review Added review suggestions by mikekistler. Consolidate. Co-authored-by: Mike Kistler --- aspnetcore/fundamentals/minimal-apis.md | 31 +++++++------------------ 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis.md b/aspnetcore/fundamentals/minimal-apis.md index 3d32e6010dbd..569b1612386a 100644 --- a/aspnetcore/fundamentals/minimal-apis.md +++ b/aspnetcore/fundamentals/minimal-apis.md @@ -78,7 +78,14 @@ Support for validation in Minimal APIs is now available. This feature allows you * Header * Request body -Validations are defined using attributes in the [`DataAnnotations`](xref:System.ComponentModel.DataAnnotations) namespace. Developers customize the behavior of the validation system by: +Validations are defined using attributes in the [`DataAnnotations`](xref:System.ComponentModel.DataAnnotations) namespace. + +When a parameter to a Minimal API endpoint is a class or record type, validation attributes are automatically applied. For example: + +```csharp +public record Product( + [Required] string Name, + [Range(1, 1000)] int Quantity); * Creating custom [`[Validation]`](xref:System.ComponentModel.DataAnnotations.ValidationAttribute) attribute implementations. * Implementing the [`IValidatableObject`](xref:System.ComponentModel.DataAnnotations.IValidatableObject) interface for complex validation logic. @@ -104,28 +111,6 @@ app.MapPost("/products", .DisableValidation(); ``` -### Validation with record types - - - -Minimal APIs also support validation with C# record types. Record types can be validated using attributes from the namespace, similar to classes. For example: - -```csharp -public record Product( - [Required] string Name, - [Range(1, 1000)] int Quantity); -``` - -When using record types as parameters in Minimal API endpoints, validation attributes are automatically applied in the same way as class types: - -```csharp -app.MapPost("/products", (Product product) => -{ - // Endpoint logic here - return TypedResults.Ok(product); -}); -``` - ## Responses Route handlers support the following types of return values: From 17a674dbe05b09b699f8aa3426ecf9a083efcaaa Mon Sep 17 00:00:00 2001 From: wadepickett Date: Mon, 19 May 2025 19:28:26 -0700 Subject: [PATCH 4/4] Added description accidently removed in review --- aspnetcore/fundamentals/minimal-apis.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/aspnetcore/fundamentals/minimal-apis.md b/aspnetcore/fundamentals/minimal-apis.md index 569b1612386a..97dfe63d41f0 100644 --- a/aspnetcore/fundamentals/minimal-apis.md +++ b/aspnetcore/fundamentals/minimal-apis.md @@ -86,6 +86,8 @@ When a parameter to a Minimal API endpoint is a class or record type, validation public record Product( [Required] string Name, [Range(1, 1000)] int Quantity); +``` +Developers customize the behavior of the validation system by: * Creating custom [`[Validation]`](xref:System.ComponentModel.DataAnnotations.ValidationAttribute) attribute implementations. * Implementing the [`IValidatableObject`](xref:System.ComponentModel.DataAnnotations.IValidatableObject) interface for complex validation logic.