Add Swagger to ASP.NET Core 3.0 Web API
Introduction
When you create a new ASP.NET Web API project, you need to present your APIs in a simple and comprehensive way? You can use Swagger.
“Swagger is a simple yet powerful representation of your RESTful API. With the largest ecosystem of API tooling on the planet, thousands of developers are supporting Swagger in almost every modern programming language and deployment environment. With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability.”
Add Swagger to Web Api Core 3 project
From the Package Manager Console window click on View > Other Windows > Package Manager Console:
Or to add Swagger to an ASP.NET Web API Core 3, we will install an open-source project called Swashbuckle.AspNetCore via NuGet.
Swagger Configuration
In the configuration file added, we find the minimum configuration needed to enable Swagger and Swagger.
We open Startup.cs file to add swagger service to middleware:
And enable the Swagger in Configure()
method.
We need to configure the launch browser in Debug from the properties of the solution.
Now, when we start a new debugging session, we get the URI of our API and we navigate to Swagger: http://localhost:[PORT_NUM]/swagger
And if we want to display our APIs:
Swagger uses many colors for each HTTP verb to distinguish API actions. Clicking on any method will give you details about accepted parameters, return type and allows you to test the method.
You can add more than a version so, they’ll be listed in the top right corner of the page, allowing users to toggle between the different documents.
GitHub source code : https://github.com/didourebai/MicroserviceSwaggerApis/tree/master/source/repos/MicroservicesSolution