C# Programming
Published in

C# Programming

Tutorial: Swagger implementation in ASP.Net Core Web API

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:

Step 1: Package installation

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

Step 2: configure Swagger middleware

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:

Step 3: Launch Swagger

  • 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.

Step 1: Package installation

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.

Step 2: configure Swagger middleware

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:

Step 3: Launch Swagger

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…. !!!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store