API documentation - Swagger UI in .NET Core

rasoolk16

Rasool Khan

Posted on March 7, 2021

API documentation - Swagger UI in .NET Core

Swagger UI

What is Swagger UI?
Swagger UI is an API documentation tool that helps us to visualize our API resources and with visual documentation, it is easier for consuming and implementing APIs.

Why Swagger?
As our backend app grows managing APIs become difficult. So it is highly advisable to create API documentation. But generating and maintaining our own API documentation is a painful task this is where Swagger UI comes into the play. The swagger UI does the hard work of generating and maintaining our API docs, making sure our documentation stays up-to-date as our API evolves.

Let's create a .Net Core app. For this tutorial, we are going to have a simple app with two basic APIs.
Alt Text

Install Swashbuckle.AspNetCore

This is a NuGet package for Swagger in dotnet. We can install it from the Nuget package manager.
Alt Text

Register Swagger UI in Startup.cs

As we know that we have to register all our services in Startup.cs file.

We will register Swagger in Startup.cs file's ConfigureServices method as shown below.
Alt Text

using Microsoft.OpenApi.Models;

Note: This using namespace has to be added to use OpenApiInfo class.

Now, we will add swagger in the Configure method as well.
Alt Text

That's it. Now we are ready to use Swagger UI for our APIs.
Alt Text

Now, we don't have to configure anything else. As we keep adding more APIs it will start to appear in this Documentation automatically.

That's the end.

Thanks for reading.
Please share and ❤️ if this was helpful.

💖 💪 🙅 🚩
rasoolk16
Rasool Khan

Posted on March 7, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related