Swagger UI Integration to .Net core Web API in 4 Steps

Swagger helps you to manage your API's from the code base itself. You don't need to go for any other third party API testing tools like Postman,Soap UI etc.

So here are the integration steps.



Step 1:
Create a .Net core web api project using visual studio

Step 2:
Install the 'Swashbuckle.AspNetCore' nuget package using Nuget package manager

Step 3:
Go to the Startup.cs file in the project and below code in the 'Configure' method.

        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json","Inventory API");
        });
Step 4: Add the below code in 'ConfigureServices' method
       services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info
            {
                Version = "v1",
                Title = "Inventory API",
                Description = "Testing"  
            });
        });

You need only these 4 steps to integrate the swagger UI.

Now run the project in visual studio and in browser add swagger after the  localhost port and hit enter
Bingo your swagger is ready to use.
https://localhost:44309/swagger/index.html

Another way is change the launch url in launchSetting.json to swagger
which is present under the project proerties

"profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }



0 comments: