Tutorial: Swagger implementation in ASP.Net Core Web API

Alpesh Patel
Jun 16 · 4 min read

In this tutorial we are going to learn how we can implement swagger in Asp.net core web API and use Swagger UI.

Prerequisites

The following must be installed in your system:

Swagger (OpenAPI) is a language-agnostic specification for describing REST APIs. It allows both computers and humans to understand the capabilities of a REST API without direct access to the source code. Its main goals are to:

  • Minimize the amount of work needed to connect decoupled services.
  • Reduce the amount of time needed to accurately document a service.

There is two way to implementations swagger for .NET Core are Swashbuckle and NSwag:

Swagger implementation using Swashbuckle:

From the Solution right click on dependency, select Manage NuGet Packages..

From the Manage NuGet Packages dialog:

  • Enter “Swashbuckle.AspNetCore” in the search box
  • Select the latest “Swashbuckle.AspNetCore” package from the Browse tab and click Install

Add the Swagger generator to the services collection in the Startup.ConfigureServices method and in the Startup.Configure method, enable the middleware for serving the generated JSON document and the Swagger UI:

  • Launch the app, and navigate to http://localhost:<port>/swagger/v1/swagger.json. The generated document describing the endpoints appears as shown in OpenAPI specification (openapi.json).
  • The Swagger UI can be found at http://localhost:<port>/swagger. Explore the API via Swagger UI and incorporate it in other programs.
  • Now using swagger you can test your API for example let’s try to post first and insert data and then get data using Get Method:
  • Click on Post Method and click on try out:
  • Now enter your data as below and click on execute button:
  • You see the response as below Image:

Swagger implementation using Swashbuckle:

To use the NSwag ASP.NET Core middleware, install the NSwag.AspNetCore NuGet package. This package contains the middleware to generate and serve the Swagger specification, Swagger UI (v2 and v3), and ReDoc UI.

From the Solution right click on dependency, select Manage NuGet Packages.

From the Manage NuGet Packages dialog:

  • Enter “ NSwag.AspNetCore” in the search box
  • Select the latest “ NSwag.AspNetCore” package from the Browse tab and click Install.

Add and configure Swagger in your ASP.NET Core app by performing the following steps.

  • In the Startup.ConfigureServices method, register the required Swagger services.
  • In the Startup.Configure method, enable the middleware for serving the generated Swagger specification and the Swagger UI:

Launch the app. Navigate to:

  • http://localhost:<port>/swagger to view the Swagger UI.
  • http://localhost:<port>/swagger/v1/swagger.json to view the Swagger specification.

Thank you for reading, please let me know your questions, thoughts, or feedback in the comments section. I appreciate your feedback and encouragement.

Keep Learning…. !!!

C# Programming

Learn, Build & Execute

C# Programming

Focuses on ASP.Net | ASP.Net Core | .Net & C# advance design, best practices & experiences to make developers life simple..!!

Alpesh Patel

Written by

Technology Specialist and Full-Stack Developer. Find me on linkedin for more details https://www.linkedin.com/in/alpesh-patel-8b930952

C# Programming

Focuses on ASP.Net | ASP.Net Core | .Net & C# advance design, best practices & experiences to make developers life simple..!!