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.
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");
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.
Use the WithTags() extension method:
using Microsoft.AspNetCore.Http;
app.MapGet("/todo", () => "Hello world").WithTags("ToDo");
or apply the Microsoft.AspNetCore.Http.TagsAttribute
instead:
using Microsoft.AspNetCore.Http;
app.MapGet("/todo", [Tags("ToDo")] () => "Hello world");