Skip to content

Instantly share code, notes, and snippets.

@mattruma
Created November 20, 2020 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattruma/6e0d86246b0da5f630383d0ec7b43f9d to your computer and use it in GitHub Desktop.
Save mattruma/6e0d86246b0da5f630383d0ec7b43f9d to your computer and use it in GitHub Desktop.
Grouping Controllers in Swagger
[SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "<Pending>")]
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddCors();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = $"{Configuration["SwaggerDocs_ProductName"]} API",
Contact = new OpenApiContact
{
Name = Configuration["SwaggerDocs_ContactName"],
Email = Configuration["SwaggerDocs_ContactEmail"]
}
});
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
c.TagActionsBy(api =>
{
if (api.GroupName != null)
{
return new[] { api.GroupName };
}
if (api.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor)
{
return new[] { controllerActionDescriptor.ControllerName };
}
throw new InvalidOperationException("Unable to determine tag for endpoint.");
});
c.DocInclusionPredicate((name, api) => true);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment