Integrating Swagger with .Net Core Web.API


Basically swagger is an open-source API testing tool. among various API testing tools, the postman is the most common and complete. but the difference between postman and swagger is, according to postman no application-level configuration is required. when it comes to the case of swagger it has to configure from the application level. in the case of swagger, once configured, it will automatically fetch all the API endpoints and resources automatically in the configured endpoint.no extra effort is required. and swagger also provides a decent way of documenting the APIs too.

Explore swagger here
Explore postman here

Basic steps to integrate swagger in a .Net core API is as follows:
  • Create Web.API Project


NuGet Packages

the  NuGet packages needed is Swashbuckle.AspNetCore package from NuGet.install this package in the created project. 


  • add necessary configurations in the startup file.
    • Add the following code to the bottom of configure method

app.UseSwagger();

app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json","swagger api");
});
    • Add the following code to the bottom of the ConfigureServices method 

services.AddSwaggerGen();

  • Add Web.API Methods 
  • Check endpoint /swagger for testing

 

 




Comments