上一篇文章给大家分享了
.NET 10 New feature 新增功能介绍-Minimal APIs增强
今天继续分享, Minimal APIs 主要应用场景
在 .NET 6--8 中,Minimal APIs 已经完成了"能用"阶段;
.NET 9--10 的目标非常明确:让 Minimal APIs 成为企业级 API 的一等公民 。
核心增强方向可以概括为四个关键词:
可维护、可治理、可扩展、可工程化
具体体现在以下能力成熟度提升:
-
更强的类型系统支持(Typed Results / OpenAPI 对齐)
-
更完善的过滤器与中间件组合能力
-
与 DI、Validation、Authorization 的深度融合
-
对 API 分层、模块化、规模化的原生支持
我们继续对比思考传统 Controller 模型的典型问题:

Minimal APIs 增强后的核心应用场景总结
场景一:高并发读接口(数据服务 / 智能问数 / 看板接口)
首先是更强 Typed Results,彻底消除"返回不确定性"
app.MapGet("/stations/{id}",
async Task<Results<Ok<StationDto>, NotFound, ProblemHttpResult>>
(long id, IStationService service) =>
{
var station = await service.GetAsync(id);
return station is null
? TypedResults.NotFound()
: TypedResults.Ok(station);
});
这保证了
-
OpenAPI 文档 100% 精确
-
AI / 前端 / SDK 自动生成零歧义
-
不再需要 [ProducesResponseType]
极致轻量,避免 Controller 反射 & Filter 链成本, 在 高 QPS 场景
-
Minimal APIs:
-
路由直接绑定 Delegate
-
少一层 MVC 管道
-
-
对 CPU 与内存友好
只读接口 + 高频接口,优先 Minimal APIs
场景二:业务能力型 API
典型问题(Controller 模式)
-
业务规则散落在 Attribute / Filter / Action
-
授权、校验、审计难以统一**.**
NET 10 的解决方式: Endpoint Filter 工程化 统一业务能力过滤器
public class OperatorContextFilter : IEndpointFilter
{
public async ValueTask<object?> InvokeAsync(
EndpointFilterInvocationContext ctx,
EndpointFilterDelegate next)
{
// 解析运营商上下文
OperatorContext.Current = Resolve(ctx.HttpContext);
return await next(ctx);
}
}
应用到业务 API
app.MapGroup("/api/charge")
.AddEndpointFilter<OperatorContextFilter>()
.RequireAuthorization("OperatorPolicy")
.MapPost("/start", StartCharge);

场景三:平台级 API 模块化
我们可以按 业务域 拆分 API,而不是 Controller:
public static class StationEndpoints
{
public static IEndpointRouteBuilder MapStation(this IEndpointRouteBuilder app)
{
var group = app.MapGroup("/api/station")
.RequireAuthorization();
group.MapGet("/{id}", GetStation);
group.MapPost("/", CreateStation);
return app;
}
}
Program.cs 极其灵活、简单做集成即可,这可以实现多业务线并行开发
app.MapStation();
app.MapOrder();
app.MapCharge();
app.MapFinance();
场景四:内部微服务 / AI Agent 接口(非传统 REST)
Minimal APIs 在 .NET 10 中,非常适合做 Agent Tool 接口:
app.MapPost("/agent/charge/analyze",
async (ChargeAnalyzeRequest req, IChargeAnalyzer analyzer) =>
{
return TypedResults.Ok(await analyzer.AnalyzeAsync(req));
});
场景五:BFF / API Gateway 层
使用 Minimal APIs 可以实现
-
聚合多个下游接口、业务逻辑编排
-
DTO 转换 + 协议适配
-
极致简洁
app.MapGet("/bff/station/{id}",
async (long id, IStationApi api, IPriceApi priceApi) =>
{
var station = await api.Get(id);
var price = await priceApi.GetPrice(id);
return TypedResults.Ok(new StationView(station, price));
});
以上是.NET 10 中 Minimal APIs 主要应用场景的总结和分享。
周国庆
2026/1/12