ASP.NET Core Minimal API Swagger tags
The ASP.NET Minimal API is currently a fairly discussed topic. Some adore him and some curse him. Time will tell if it finds a real use.
I was interested in new extensions methods over IEndpointRouteBuilder
that make it easy to add a new endpoint.
app.MapGet("/todo", () => "Hello world")
This can be helpful if you need to automatically generate an endpoints. For example, if you want to generate CRUD endpoints based on your domain classes. In this case, you would like to generate documentation for the given endpoints.
Swashbuckle.AspNetCore will generate it for you, unfortunately all added endpoints will be grouped by one tag.
Possible solution
As a workaround, you can use the TagActionsBy
method when configuring the swagger generator.
You can use RelativePath
as a naive solution, for example:
builder.Services.AddSwaggerGen(c =>
{
c.TagActionsBy(d =>
{
return new List<string>() { d.RelativePath! };
});
});
Deciding on the basis of RelativePath
can be complicated. A better solution may be to use DisplayName
.
app.MapGet("/api/catalogs/{id}", () => "Hello world").WithDisplayName("Catalogs");
builder.Services.AddSwaggerGen(c =>
{
c.TagActionsBy(d =>
{
return new List<string>() { d.ActionDescriptor.DisplayName! };
});
});
UPDATED 2.1. 2021
New .WithTags ("Catalogs")
extension method have been added in version 6.0.0
(not RC).
So just call
app.MapGet("/api/catalogs/{id}", () => "Hello world").WithTags("Catalogs");
Thanks to @captainsafia. PR 2210