Skip links
swagger dot net

Swagger in .NET Core APIs

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.
swagger in .net

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.

Summary

In this article, we learned. How to use swagger with .NET core web Api’s.