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

zlwx9yxi  于 11个月前  发布在  .NET
关注(0)|答案(2)|浏览(116)

我有一个需要使用最小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", () => { ... });

字符串
将属性放在终结点中是有效的,但它不起作用。
代码:

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


你知道我错过了什么吗?

qxsslcnc

qxsslcnc1#

试试这个:
在你的端点后添加ExcludeFromDescription()方法,它对我很有效。

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

字符串

jm2pwxwz

jm2pwxwz2#

您可以使用IActionModelConvention来隐藏端点。阅读有关dev.to的文章。

相关问题