Search code examples
asp.net-coreswaggerswashbuckle.aspnetcore

ASP.NET Core 6.0 Minimal API / Swagger tags


Is there any way to change the tag for a given http method written with minimal api?

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

...

app.MapGet("/todo", () => "Hello world");

So that I can logically group methods in the documentation, similar to those that were when they were in the controllers.

Edited

I tried (hint from @Modar Na) SwaggerOperation, unfortunately it didn't help.

app.MapGet("/todo", [SwaggerOperation(Tags = new[] { "ToDo" })]() => "Hello world");
app.MapPost("/todo", [SwaggerOperation(Tags = new[] { "ToDo" })]() => "Hello world");

app.MapGet("/projects", [SwaggerOperation(Tags = new[] { "Projects" })]() => "Hello world");
app.MapPost("/projects", [SwaggerOperation(Tags = new[] { "Projects" })]() => "Hello world");

enter image description here

Updated

As a workaround, I used the TagActionsBy method when configuring the swagger generator.

builder.Services.AddSwaggerGen(c =>
{
    c.TagActionsBy(d =>
    {
        return new List<string>() { d.ActionDescriptor.DisplayName! };
    });
});

See my blog post.


Solution

  • How about WithTags:

    app.MapGet("/todo", () => "Hello world").WithTags("ToDo");