Shimakaze.MinimalApi.Plus.SourceGenerator
0.1.3
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Shimakaze.MinimalApi.Plus.SourceGenerator --version 0.1.3
NuGet\Install-Package Shimakaze.MinimalApi.Plus.SourceGenerator -Version 0.1.3
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="Shimakaze.MinimalApi.Plus.SourceGenerator" Version="0.1.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Shimakaze.MinimalApi.Plus.SourceGenerator" Version="0.1.3" />
<PackageReference Include="Shimakaze.MinimalApi.Plus.SourceGenerator" />
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 Shimakaze.MinimalApi.Plus.SourceGenerator --version 0.1.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Shimakaze.MinimalApi.Plus.SourceGenerator, 0.1.3"
#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 Shimakaze.MinimalApi.Plus.SourceGenerator@0.1.3
#: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=Shimakaze.MinimalApi.Plus.SourceGenerator&version=0.1.3
#tool nuget:?package=Shimakaze.MinimalApi.Plus.SourceGenerator&version=0.1.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Shimakaze.MinimalApi.Plus
一个用于简化 .NET Minimal API 开发的工具库,通过 Source Generator 提供更优雅的 API 端点定义方式。
特性
- 基于 Source Generator 的编译时代码生成
- 支持依赖注入
- 类型安全的路由定义
- 简洁的 API 端点组织方式
- AOT 兼容(.NET 9.0 及以上版本)
快速开始
1. 安装
dotnet add package Shimakaze.MinimalApi.Plus
dotnet add package Shimakaze.MinimalApi.Plus.SourceGenerator
2. 创建 API 端点
[ApiEndpoints]
public sealed class SimpleApiEndpoints(SimpleServices services) : ApiEndpoints
{
[HttpGet("/weatherforecast")]
public WeatherForecast[] GetWeatherForecasts()
=> services.GetWeatherForecasts();
}
3. 注册服务
var builder = WebApplication.CreateBuilder(args);
// 添加服务
builder.Services.AddTransient<SimpleServices>();
// 添加端点
builder.Services.AddEndpoints();
var app = builder.Build();
// 映射端点
app.MapEndpoints();
app.Run();
核心概念
ApiEndpoints 基类
提供对 HttpContext 的便捷访问:
public abstract class ApiEndpoints
{
protected HttpContext Context { get; set; }
protected HttpRequest Request => Context.Request;
protected HttpResponse Response => Context.Response;
// ... 其他便捷属性
}
ApiEndpointsAttribute
标记类为 API 端点集合:
[AttributeUsage(AttributeTargets.Class)]
public sealed class ApiEndpointsAttribute : Attribute
{
}
AOT 兼容性
- .NET 9.0 及以上版本:完全支持 AOT
- .NET 8.0 及更早版本:不支持 AOT,因为依赖 TypeDescriptor 的类型转换
限制
- 不支持泛型类型
- 不支持抽象类型
- .NET 8.0 及更早版本不支持 AOT
许可证
There are no supported framework assets in this package.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.