ASP.NET Core 6.0 Minimal API / Swagger tags

Viewed 2638

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.

2 Answers

I think that all operations that share the same route will get grouped by default

but for explicitly defining the groups you'll need to use the swagger tag which can be used like the this:

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

link for controller approach

How about:

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