Location>code7788 >text

Aspnet Core 10 Preview3 has provided parameter verification support for the minimum API

Popularity:861 ℃/2025-04-11 12:31:01

Preface

I believe everyone has used Minimal API more or less, which is fast and concise, and has a burst of performance. It is the best choice for quickly developing API ports! However, so far, the minimum API does not yet support built-in verification support for request parameters, such as[Required], or wherever we need to verify, we often need to independently expand an EndpointFilter to implement such logic. This is a big and small point in the NET community, but MS finally came out and provided the third preview version of NET10.Built-in verification support!

Experience

Install and upgrade to the latest released NET10Preview3, and then complete the following operations:

();

Configure the InterceptorsNamespaces property in the project file (.csproj) to enable the automatically generated interceptor. The example configuration is as follows:

<PropertyGroup>
   <!-- Enable generation of verification attribute interceptor -->
   <InterceptorsNamespaces>$(InterceptorsNamespaces);</InterceptorsNamespaces>
 </PropertyGroup>

ThenSource GeneratorIt will work, and the verification type will be automatically discovered. The implementation mechanism will automatically find the verification properties configured in the type defined in the minimum API handler or its base type, and perform verification on each endpoint via the added endpoint filter criteria. This approach greatly simplifies the workload of developers without manually writing verification logic.

For example, the following API definition:

("/products",
     ([EvenNumber(ErrorMessage = "Product ID must")] int productId, [Required] string name)
         => (productId))

If the product ID is not transmitted or the name is empty, it will returnHTTP 400 Bad RequestThe error!

If you need to exclude the Endpoint parameter verification, you only need to add.DisableValidation()Just:

("/products",
     ([EvenNumber(ErrorMessage = "Product ID must")] int productId) => (productId))
     .DisableValidation();

Of course, except built-inIn addition to the verification features, you can fully expand your verification features, such as complex verification situations related to business, and you only need to implement them.IValidatableObjectInterface can be inherited fromValidationAttributeImplement some simple personalized verification

at last

The verification support mechanism enables MinimalAPI to automatically perform verification before requesting data into business logic, thereby improving security and code maintenance. At the same time, developers can use built-in verification functions, or use custom verification attributes and interface implementations to meet specific business needs. With this flexible design, Core's MinimalAPI can provide powerful and reliable verification support while keeping it simple.

It is currently an early preview version. There may be some changes in the subsequent release of the official version. You can only understand it in the early stage and then use it for production after it is officially released~

Let’s look forward to the early arrival of the official version!