Build Up Swagger In ASP.Net Core 2.2 Web API

Build up swagger in asp.net core 2.2 Web API

What is Swagger?

Swagger is the largest framework for designing APIs using a common language and enabling the development across the whole API lifecycle, including documentation, design, testing, and deployment.

The framework provides a set of tools that help programmers generate client or server code and install self-generated documentation for web services.

What is Swashbuckle?

Swashbuckle is a handy library to easily bring Swagger support to your ASP.NET Core (or ASP.NET) application. It is especially handy when developing an HTTP based API. It creates a form of interactive documentation based on the OpenAPI Specification.

Add Swagger to ASP.NET Core 2.2 Web API

Create an ASP .NET Core 2.2 Web API project using Visual Studio 2019.

After creating the project, go to Nuget Package Manager and add Swashbuckle.AspNetCore package.

Build up swagger in asp.net core 2.2 Web API

Open Startup.cs file to add swagger service to middleware. Like:

Build up swagger in asp.net core 2.2 Web API

And enable the Swagger UI in Configure() method.

Build up swagger in asp.net core 2.2 Web API

To open Swagger UI as your home page.In Debug tab from the properties of the solution, you will find a check box “Launch browser”. Change the value in a text box with “swagger”.

Build up swagger in asp.net core 2.2 Web API

Now, when you run the application you should see Swagger UI for ValuesController.

Build up swagger in asp.net core 2.2 Web API

Manage versions of APIs

You can manage different versions of your APIs, for example, you have v2 APIs as well and you need to enable that in Swagger then you just need to add a couple of steps.

First, add new SwaggerDoc in ConfigureService method and then add new endpoint in Configure method in Startup.cs

Build up swagger in asp.net core 2.2 Web API

Build up swagger in asp.net core 2.2 Web API

To Include XML Comments in Swagger, we recommend reading this article Include XML Comments in Swagger under ASP.NET Core 2.2.

The source code for this tutorial is available on GitHub


Similar Articles