How to change Controller’s name in Swagger UI

Group swagger methods

Today in this article, we shall see how to Change Controller’s name in Swagger UI using a simple and very effective technique.

Today in this article, we will cover below aspects,

We shall use SwaggerOperation with Tag argument which is defined in swagger annotation details while using the Swashbuckle component.

Change in controller name in Swagger might be needed for better management of multiple API based on the Domain or Subdomain (If following Domain-driven design) or Based on the functionalities of APIs etc.

If you already have enabled generic Swagger Documentation, then please see the steps mentioned below to enable the Swagger Annotations.

Above we shall see multiple uses of annotation,

  • Change Default Controller name.
  • Group Various routes methods from the same controller to context-based Names
  • Group Various routes methods from different Controller under the same annotation
  • Provide a meaningful name and group them as required to help Developers to understand the methods.

If looking for basics on how to enable Swagger from the start then please visit the article which explains simple 2-3 steps.

Install and Enable Annotations

Install the following Nuget package into your ASP.NET Core application.

Nuget Package Manager

>Install-Package Swashbuckle.AspNetCore.Annotations 

Using CLI:

>dotnet add package Swashbuckle.AspNetCore.Annotations

In the ConfigureServices method of Startup.cs, enable annotations within the Swagger config block,

services.AddSwaggerGen(c => { ... c.EnableAnnotations(); });

Example:

Change Controller's name in Swagger

Group Swagger route methods using Tags

One can easily group the various Swagger methods using Tags and SwaggerOperation annotations.

Before adding Annotation

swagger annotations example

After adding Annotation

Example:

[SwaggerOperation(Tags = new[] { "Checking Account" })]
[SwaggerOperation(Tags = new[] { "Saving Account" })]

group methods in swagger
Change Controller's name in Swagger

Above we are able to customize and group together various API endpoints based on the custom domain/functionalities/subdomain or business functionalities.

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *