ASP.NET Core 6 - 从 swagger 资源管理器中隐藏最小 API 端点

Jef*_*eff 4 asp.net swagger

我有一个需要使用最少 API 的实现。但不知何故,无法将其从 swagger API 浏览器中排除。在 MVC 控制器方法中,我们可以使用 隐藏端点[ApiExplorerSettings(IgnoreApi=true)],但对于最小 API 来说并非如此。

代码:

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("api/v1/endpoint_a", () => { ... });

// Hide this from Swagger API explorer
app.MapGet("api/v1/endpoint_b", () => { ... });
Run Code Online (Sandbox Code Playgroud)

将属性放入端点是有效的,但不起作用。

代码:

app.MapGet("api/v1/endpoint_b", [ApiExplorerSettings(IgnoreApi=true)]() => { ... });
Run Code Online (Sandbox Code Playgroud)

知道我在这里缺少什么吗?

小智 20

试试这个:在端点之后
添加方法,它对我有用。ExcludeFromDescription()

app.MapPost("api/newUser", [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Roles = "Administrator")]
        (string email, string password, int idUserGroup) =>
        {
           //Some code here
        }).ExcludeFromDescription();
Run Code Online (Sandbox Code Playgroud)