Reputation: 1704
I use Swagger in my .NET 6.0 API, and I want to use different SwaggerEndpoint to group my API. Here is the code I set in Program.cs
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "Blog API",
Version = "v1"
});
options.SwaggerDoc("v2", new OpenApiInfo
{
Title = "Blog API",
Version = "v2"
});
}
// ...
if (app.Environment.IsDevelopment())
{
app.UseSwaggerUI(options =>
{
options.InjectStylesheet("/swagger-ui/custom.css");
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Blog API V1");
options.SwaggerEndpoint("/swagger/v2/swagger.json", "Blog API V2");
});
}
And I set Attribute [ApiExplorerSettings(GroupName = "v2")]
or v1 in controller.
In swagger UI, when I select the option, it cannot filter my API (It still display all API both in two options).
Upvotes: 5
Views: 3128
Reputation: 1704
Finally, I remove this setting in swagger, and ApiExplorerSettings(GroupName = "v2")] is working!
builder.Services.AddSwaggerGen(options => {
// remove this setting
options.DocInclusionPredicate((name, api) => true);
});
The setting means always show all API when select the definition on upper right corner.
Sorry about it's hard to paste all Program.cs
code (too long) in my question. Hope this answer can help someone.
Upvotes: 5