MinimalApi.Extensions.Validation
1.0.0-beta
This is a prerelease version of MinimalApi.Extensions.Validation.
dotnet add package MinimalApi.Extensions.Validation --version 1.0.0-beta
NuGet\Install-Package MinimalApi.Extensions.Validation -Version 1.0.0-beta
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MinimalApi.Extensions.Validation" Version="1.0.0-beta" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MinimalApi.Extensions.Validation" Version="1.0.0-beta" />
<PackageReference Include="MinimalApi.Extensions.Validation" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MinimalApi.Extensions.Validation --version 1.0.0-beta
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: MinimalApi.Extensions.Validation, 1.0.0-beta"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MinimalApi.Extensions.Validation@1.0.0-beta
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MinimalApi.Extensions.Validation&version=1.0.0-beta&prerelease
#tool nuget:?package=MinimalApi.Extensions.Validation&version=1.0.0-beta&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
MinimalApi.Extensions.Validation
基于 FluentValidation
对 MinimalApi
endpoint 参数自动校验,并短路返回请求
安装
dotnet add package MinimalApi.Extensions.Validation
配置
// 需要提前注册所有的validator,FluentValidation提供了反射注入
// 如果你需要aot版本,在文档下面会介绍
services.AddAutoValidation();
// 自定义返回
services.AddAutoValidation(o =>
{
o.ValidationResultCreator = (validationResult, httpContext) =>
{
httpContext.Response.StatusCode = StatusCodes.Status200OK;
return validationResult.Errors;
};
});
使用
public class Cat
{
public string Name { get; set; }
}
public class CatValidator : AbstractValidator<Cat>
{
public CatValidator()
{
RuleFor(x => x.Name).NotEmpty().MinimumLength(3).MaximumLength(8);
}
}
app.MapPost("create-cat", (Cat cat) =>
{
return TypedResults.Ok(cat);
}).AddValidationFilter();
泛型版本
app.MapPost("create-cat", (Cat cat) =>
{
return TypedResults.Ok(cat);
}).AddValidationFilter<Cat>();
说明
- 非泛型版本会将所有
endpoint
传入的class
参数查找对应的validator
- 如果想使用
aot
注入所有的validator
,请安装MinimalApi.Extensions.SourceGeneration
库
aot 注入 validator
安装并设置代码生成器
<ItemGroup>
<PackageReference Include="MinimalApi.Extensions.SourceGeneration" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
</ItemGroup>
代码注入
builder.Services.AddAutoValidation()
// 自动注入所有的validator
.RegisterAllValidators();
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net9.0
- FluentValidation (>= 11.9.2)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on MinimalApi.Extensions.Validation:
Package | Downloads |
---|---|
MinimalApi.Extensions
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
1.0.0-beta | 174 | 4/20/2025 |