MinimalEndpoints.Generator
1.1.0
dotnet add package MinimalEndpoints.Generator --version 1.1.0
NuGet\Install-Package MinimalEndpoints.Generator -Version 1.1.0
<PackageReference Include="MinimalEndpoints.Generator" Version="1.1.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="MinimalEndpoints.Generator" Version="1.1.0" />
<PackageReference Include="MinimalEndpoints.Generator"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add MinimalEndpoints.Generator --version 1.1.0
#r "nuget: MinimalEndpoints.Generator, 1.1.0"
#:package MinimalEndpoints.Generator@1.1.0
#addin nuget:?package=MinimalEndpoints.Generator&version=1.1.0
#tool nuget:?package=MinimalEndpoints.Generator&version=1.1.0
MinimalEndpoints
A compile-time source generator for ASP.NET Core that auto-registers Minimal API endpoints — zero reflection, zero boilerplate.
一个编译时 Source Generator,为 ASP.NET Core 自动注册 Minimal API 端点 —— 无反射、无样板代码。
✨ Features / 功能特性
| Feature | Description |
|---|---|
| Convention-based discovery | Any class inheriting ApiEndpointBase or implementing IApiEndpoint is auto-registered |
| Attribute-based control | Use [ApiEndpoint] to set lifetime, ordering, or skip endpoints |
| Assembly-level options | Customize generated class/namespace/method names via [ApiEndpointsOptions] |
| Zero reflection | Everything is resolved at compile time via Roslyn incremental source generator |
| Multiple overloads | Generated MapApiEndpoints supports both WebApplication and IEndpointRouteBuilder |
| DI lifecycle control | Singleton (default), Scoped, or Transient per-endpoint |
| Method-level attributes | [HttpGet/Post/Put/Delete/Patch] + optional [RoutePrefix] on static methods — no Register override needed |
| 功能 | 说明 |
|---|---|
| 约定式发现 | 继承 ApiEndpointBase 或实现 IApiEndpoint 的类自动注册 |
| 特性配置 | 使用 [ApiEndpoint] 设置生命周期、排序、跳过 |
| 程序集级选项 | 通过 [ApiEndpointsOptions] 自定义生成的类名/命名空间/方法名 |
| 零反射 | 全部在编译时通过 Roslyn 增量 Source Generator 解析 |
| 多重载 | 生成的 MapApiEndpoints 同时支持 WebApplication 和 IEndpointRouteBuilder |
| DI 生命周期 | 支持 Singleton(默认)、Scoped、Transient 逐端点控制 |
| 方法级特性 | 静态方法上使用 [HttpGet/Post/Put/Delete/Patch],可选 [RoutePrefix],无需重写 Register |
📦 Installation / 安装
dotnet add package MinimalEndpoints.Abstractions
dotnet add package MinimalEndpoints.Generator
Or add to your .csproj / 或直接编辑 .csproj:
<ItemGroup>
<PackageReference Include="MinimalEndpoints.Abstractions" Version="*" />
<PackageReference Include="MinimalEndpoints.Generator" Version="*"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>
🚀 Quick Start / 快速上手
1. Create an endpoint / 创建端点
using MinimalEndpoints.Abstractions;
using Microsoft.AspNetCore.Routing;
public class HelloEndpoint : ApiEndpointBase
{
public override void Register(IEndpointRouteBuilder routes)
{
routes.MapGet("/hello", () => new { Message = "Hello!" });
routes.MapGet("/hello/{name}", (string name) => new { Message = $"Hello, {name}!" });
}
}
2. Wire up in Program.cs / 在 Program.cs 中接线
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddApiEndpoints(); // DI registration (auto-generated)
var app = builder.Build();
app.MapApiEndpoints(); // Route mapping (auto-generated)
app.Run();
That's it! All endpoints are discovered and registered at compile time.
就这样!所有端点在编译时自动发现并注册。
Method-attribute routing / 方法级特性路由
For simple endpoints, place HTTP method attributes on public static methods. ASP.NET Core still owns parameter binding ([FromBody], [AsParameters], DI services, CancellationToken, etc.).
简单端点可以直接在 public static 方法上标注 HTTP 动词特性;参数绑定仍交给 ASP.NET Core 处理。
using Microsoft.AspNetCore.Http;
using MinimalEndpoints.Abstractions;
[RoutePrefix("/api/items")]
public class ItemEndpoints
{
[HttpGet("")]
public static IResult List() => Results.Ok();
[HttpGet("{id:int}")]
public static IResult Get(int id) => Results.Ok(new { id });
[HttpPost("")]
public static IResult Create([Microsoft.AspNetCore.Mvc.FromBody] ItemDto dto) => Results.Created("/api/items/1", dto);
}
If a file also imports MVC attributes such as Microsoft.AspNetCore.Mvc.HttpGetAttribute, fully qualify the MinimalEndpoints HTTP attributes or use aliases to avoid ambiguous attribute names.
📖 Configuration / 配置
Per-endpoint / 端点级配置
[ApiEndpoint(
Lifetime = EndpointLifetime.Scoped, // DI lifecycle / DI 生命周期
Order = 10, // Registration order / 注册顺序
Skip = false // Skip auto-registration / 跳过自动注册
)]
public class MyEndpoint : ApiEndpointBase { ... }
Assembly-level / 程序集级配置
using MinimalEndpoints.Abstractions;
[assembly: ApiEndpointsOptions(
GeneratedNamespace = "MyApp", // Output namespace / 输出命名空间
GeneratedClassName = "EndpointExtensions", // Output class name / 输出类名
AddMethodName = "AddEndpoints", // DI method name / DI 方法名
MapMethodName = "MapEndpoints", // Route method name / 路由方法名
DefaultLifetime = EndpointLifetime.Scoped, // Default lifetime / 默认生命周期
EmitWebApplicationOverload = true, // Generate WebApplication overload
EmitEndpointRouteBuilderOverload = true // Generate IEndpointRouteBuilder overload
)]
Endpoint discovery / 端点发现机制
| Method | Description |
|---|---|
Inherit ApiEndpointBase |
Default convention / 默认约定 |
Implement IApiEndpoint |
Interface convention / 接口约定 |
[ApiEndpoint] attribute |
Explicit marking / 显式标记 |
Custom BaseType/InterfaceType |
Via [ApiEndpointsOptions] / 通过程序集选项自定义 |
📂 Project Structure / 项目结构
MinimalEndpoints/
├── src/
│ ├── MinimalEndpoints.Abstractions/ # Runtime types (net8.0)
│ └── MinimalEndpoints.Generator/ # Source Generator (netstandard2.0)
├── tests/
│ └── MinimalEndpoints.Tests/ # Generator unit tests
├── samples/
│ └── SampleWebApi/ # Usage example
└── .github/workflows/ci.yml # CI/CD pipeline
🔧 Packages / 包说明
| Package | Target | Description |
|---|---|---|
MinimalEndpoints.Abstractions |
net8.0 |
Runtime interfaces, attributes, base classes / 运行时接口、特性、基类 |
MinimalEndpoints.Generator |
netstandard2.0 |
Roslyn incremental source generator / Roslyn 增量源代码生成器 |
🏗️ CI/CD
- Build & Test — triggered on every push and PR to
main - Publish to NuGet — triggered when a version tag
v*is pushed (only if tests pass)
git tag v1.0.0 && git push origin v1.0.0
📄 License
MIT
Learn more about Target Frameworks and .NET Standard.
This package has 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.