VOOZH about

URL: https://www.nuget.org/packages/CloudYxt.MinimalApi/

⇱ NuGet Gallery | CloudYxt.MinimalApi 1.9.0




👁 Image
CloudYxt.MinimalApi 1.9.0

dotnet add package CloudYxt.MinimalApi --version 1.9.0
 
 
NuGet\Install-Package CloudYxt.MinimalApi -Version 1.9.0
 
 
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="CloudYxt.MinimalApi" Version="1.9.0" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CloudYxt.MinimalApi" Version="1.9.0" />
 
Directory.Packages.props
<PackageReference Include="CloudYxt.MinimalApi" />
 
Project file
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 CloudYxt.MinimalApi --version 1.9.0
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: CloudYxt.MinimalApi, 1.9.0"
 
 
#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 CloudYxt.MinimalApi@1.9.0
 
 
#: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=CloudYxt.MinimalApi&version=1.9.0
 
Install as a Cake Addin
#tool nuget:?package=CloudYxt.MinimalApi&version=1.9.0
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

云享通.Net Core针对MinimalApi常规操作库

以Program.cs为例的使用方法

#pragma warning disable IL2026, IL3050
//AOT编译需要的JSON序列化类型
AppJsonSerializer.TypeInfoResolver = AppJsonSerializerContext.Default;

ApiMessageLogFilter.ApiName = "testApi";
//日志回调处理
ApiMessageLogFilter.CallBack = (log) =>
{
 Console.WriteLine("---------");
 Console.WriteLine(JsonSerializer.Serialize(log, AppJsonSerializer.JsonOptions));
};

//用户认证方法,认证成功时返回用户对象
ApiMessageAuthorizationFilter.ApiAuthorization = (policy, roles, authSchemes, context, scheme, param) =>
{
 if (param != "expected-token")
 throw new ApiMessageException(403, "无效令牌");

 return new { id = "admin" };
};
#pragma warning restore IL2026, IL3050

var builder = WebApplication.CreateSlimBuilder(args);
builder.WebHost.UseUrls("http://*:5004");
//配置JSON输出不使用驼峰CamelCase模式
builder.Services.UseJsonWithoutCamelCase();
builder.Services.ConfigureHttpJsonOptions(options =>
{
 //注册AOT编译需要的JSON序列化类型
 options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializer.TypeInfoResolver);
});

var app = builder.Build();
//全局错误输出的包装方式,现已有四种包装模式
app.UseExceptionHandler(ExceptionHandlers.HandleGlobalToCodeMessage);
//app.UseExceptionHandler(ExceptionHandlers.HandleGlobalToMessageInfo);
//app.UseExceptionHandler(ExceptionHandlers.HandleGlobalToStatusMessage);
//app.UseExceptionHandler(ExceptionHandlers.HandleGlobalToStatusMessageData);

//定义最基本的api过滤器绑定(注意:过滤器的顺序)
var api = app.MapGroup("/")
 //将接口参数的DateTime统一转换成本地DateTime
 .AddEndpointFilter<UtcToLocalDateTimeFilter>()
 //日志过滤器
 .AddEndpointFilter<ApiMessageLogFilter>()
 //将错误输出的包装方式
 .AddEndpointFilter<PackProblemToCodeMessageFilter>()
 //捕获API中的throw new ApiMessageException(10, "错误");
 .AddEndpointFilter<ApiMessageExceptionFilter>();

//GET的参数验证
api.MapGet("/authget", ([AsParameters] request_authget request) => $"auth get id:{request.id}")
 //将接口参数的DateTime统一转换成本地DateTime
 .WithUtcToLocalDateTime()
 //使用的验证
 .WithValidationFilter<request_authget>();

//POST的参数验证
api.MapPost("/authpost", (HttpContext context, [Description("The name of the person to greet.")] request_auth request) =>
 {
 //获取当前已验证登录的用户对象
 var userTag = context.currentUserTag<T>();
 //获取当前已验证登录用户使用的Scheme
 var userScheme = context.currentUserScheme();

 if (request.name == "err")
 throw new ApiMessageException(10, "错误");

 return Results.Content($"authpost:{request.name}");
 })

 //若需忽略错误包装
 //.WithMetadata(new PackProblemIgnoreAttribute())
 //若需忽略用户验证
 //.WithMetadata(new AllowAnonymousAttribute())

 //用户认证处理
 //验证用户
 .WithApiMessageAuthorization()
 //自定义验证用户
 .WithApiMessageAuthorization(policy: "", schemes: "Bearer,Auth", roles: "0,1")

 //模型验证处理
 .WithValidationFilter<request_auth>()
 
 //关闭ApiMessageLog的日志记录(若需要)
 //.DisableApiMessageLog()

 //关闭FORM提交的防伪验证
 .DisableAntiforgery();

app.Run();

public class request_authget
{
 [Required]
 public string id { get; set; }

 [Range(1, 2)]
 public int num { get; set; }
}

public class request_auth
{
 public int id { get; set; }
 [Required(ErrorMessage = "名称必须填写")]
 public string name { get; set; }
}

//AOT需要的模型的JSON序列化扩展
[JsonSerializable(typeof(request_auth))]
[JsonSerializable(typeof(request_authget))]
internal partial class AppJsonSerializerContext : JsonSerializerContext
{

}

跨域中间件使用

在Startup的Configure中定义

//支持所有域
app.UseMiddleware<AllowDomainCorsHeaderMiddleware>();

//支持一个域名跨域
app.UseMiddleware<AllowDomainCorsHeaderMiddleware>("https://www.domain1.com");

//支持多个域名跨域
app.UseMiddleware<AllowDomainCorsHeaderMiddleware>("https://www.domain1.com,https://www.domain2.com");

基于HttpContext的扩展

var ip = HttpContext.remoteRealIp(); //来源IP
var from = HttpContext.remoteFrom(); //来源URL
var agent = HttpContext.remoteAgent(); //来源AGENT
Product Versions Compatible and additional computed target framework versions.
.NET net8.0 net8.0 is compatible.  net8.0-android net8.0-android was computed.  net8.0-browser net8.0-browser was computed.  net8.0-ios net8.0-ios was computed.  net8.0-maccatalyst net8.0-maccatalyst was computed.  net8.0-macos net8.0-macos was computed.  net8.0-tvos net8.0-tvos was computed.  net8.0-windows net8.0-windows was computed.  net9.0 net9.0 was computed.  net9.0-android net9.0-android was computed.  net9.0-browser net9.0-browser was computed.  net9.0-ios net9.0-ios was computed.  net9.0-maccatalyst net9.0-maccatalyst was computed.  net9.0-macos net9.0-macos was computed.  net9.0-tvos net9.0-tvos was computed.  net9.0-windows net9.0-windows was computed.  net10.0 net10.0 was computed.  net10.0-android net10.0-android was computed.  net10.0-browser net10.0-browser was computed.  net10.0-ios net10.0-ios was computed.  net10.0-maccatalyst net10.0-maccatalyst was computed.  net10.0-macos net10.0-macos was computed.  net10.0-tvos net10.0-tvos was computed.  net10.0-windows net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on CloudYxt.MinimalApi:

Package Downloads
CloudYxt.MinimalApi.Swagger

云享通.Net Core针对MinimalApi常规操作库,支持AOT编译;同时整合扩展swagger

CloudYxt.MinimalApi.OpenApi

云享通.Net Core针对MinimalApi常规操作库,支持AOT编译;同时整合扩展Microsoft.AspNetCore.OpenApi

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.9.0 141 4/15/2026
1.8.0 264 12/5/2025
1.7.1 375 9/19/2025
1.6.2 271 6/23/2025
1.5.0 250 6/20/2025
1.3.0 434 6/16/2025
1.2.0 392 6/13/2025
1.1.0 390 6/12/2025
1.0.0 302 4/24/2025

云享通.Net Core针对MinimalApi常规操作库,详细使用请常见README.md

1.9
更改基础信息,更新依赖

1.8
改进跨域中间件AllowDomainCorsHeaderMiddleware

1.7.1
增加UtcToLocalDateTimeFilter,或直接使用WithUtcToLocalDateTime(),将接口参数的DateTime统一转换成本地DateTime

1.6.2
修正日志收集时的JSON序列化错误

1.5
新增app快捷方法:DisableApiMessageLog()关闭ApiMessageLog的日志记录

1.3
修正AOT和非AOT的JSON序列化错误

1.2
增加在API内获取当前已登录用户对象的方法 currentUserTag<T>(); 和 currentUserScheme();

1.1
增加builder.Services.UseJsonWithoutCamelCase(); 配置JSON输出不使用驼峰CamelCase模式