(C# ASP.NET Core) Configuring and Using Swagger to Run WebApi

Let us now configure our WebApi project to add support for SwaggerUI. As we told earlier, SwaggerUI is provided by both SwashBuckle and NSwag. We can use either of the two - and I shall be using SwashBuckle.

Categories | About |     |  

Parveen,

Install Nuget Package for SwashBuckle

Open the WebApi project that we have been completing in this chapter. Locate the program.cs file, and double click to open it.

First we have listed the various nuget packages that must be installed. These have been listed for your convenience so that you can easily set your own WebApi projects. The first two have already been added, and now we should add the third one for SwashBuckle.

// Install-Package Microsoft.AspNetCore.Identity.EntityFrameworkCore 
// Install-Package Microsoft.EntityFrameworkCore.Sqlite 
// Install-Package Swashbuckle.AspNetCore 
using Microsoft.EntityFrameworkCore;

using MyRazorApp.Models;

var builder = WebApplication.CreateBuilder();

// the database is created in memory 
builder.Services.AddDbContext<MyApiContext>(
  opt => opt.UseSqlite("DataSource=file:mem?mode=memory&cache=shared")
);

// . . . remaining file 

Open the package manager console and type the Install-Package Swashbuckle.AspNetCore command. Hit enter and allow it to complete.


Install-Package Swashbuckle.AspNetCore

Come back to the program.cs file again. Just after the service for razor pages add a service for swagger generator as you see here.

// . . . program.cs file 
builder.Services.AddRazorPages();

// --- 1 -- add this 
builder.Services.AddEndpointsApiExplorer();

// --- 2 -- add this 
builder.Services.AddSwaggerGen();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{

  // --- 3 -- add these 
  app.UseSwagger();

  app.UseSwaggerUI();

}

// . . . remaining file 

Next we have enabled the middleware for serving the SwaggerUI.

These are the only additions required for enabling SwaggerUI in our project.

Video Explanation (see it happen!)

Please watch the following youtube video:

Run the Application Now!

Let us test the swagger now. Run the application and allow the home page to open.

Take note the port number, and type the following in the web browser - localhost:<port>/swagger


https://localhost:<port>/swagger

Hit enter, and we observe that a Swagger UI opens. SwashBuckle has used reflection to obtain the list of all your web api.

Let us view it in full screen!

As you can see, all the various web api can be seen listed here. There is a color for each type of API - blue, red, green, etc., (see the linked video)

Let's try to run the first API. Click the drop down. Next, click the "Try it Out" button! We observe that the execute button opens! Click the execute button. We observe that the server sends 200 OK response along with the records. Similarly, we can execute the other API also!


This Blog Post/Article "(C# ASP.NET Core) Configuring and Using Swagger to Run WebApi" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.