Swagger in .NET Core APIs
Prakash Software Pvt. Ltd.
Swagger in .NET Core APIs

Overview

If you are a developer or tester, understanding various methods is quite challenging sometimes when building or consuming application.

If you are using Web Api or mvc for restful Apis, you definitely want to use Swagger.

using swagger with .net core is as easy as adding couple of nuget packages and modifying startup.cs

1. Create .NetCore WebApi sample application

2. Install NuGet Packages :  Install-Package Swashbuckle.AspNetCore

3. Configure swagger in startup.cs file :

  • Add swagger generator to the service collection after services.AddMvc(); in ConfigureServices() method.

services.AddSwaggerGen(c =>  

   c.SwaggerDoc("v1"new Info { Title = "My API", Version = "v1" });  
});

  • Enable middleware to serve generated swagger as JSON endpoint after      app.UseStaticFiles();

app.UseSwagger();

  • Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.

 app.UseSwaggerUI(c => 
 { 
    c.SwaggerEndpoint("/swagger/v1/swagger.json""My API V1");  
});

REPORT THIS AD

4. Setup completed now run the project. we are ready to go.

img19

your local port number will different than mine, use your port number with slash swagger.

Click on try it out, by giving expected request parameters you will get response in response body section.

img193

Summary

In this article, we learned.

  • How to use swagger with .Net core web Apis.

Related Blogs