Swashbuckle and ASP.NET Core

#webapi #asp.net #c# #.netcore #swagger

Manoj Maharana Oct 06 2020 · 1 min read
Share this

Step: 1

  Go to Solution > Manage Nuget Packages for Solutions

Step: 2

To use Swagger, you must need to install the following library.

"Swashbuckle.AspNetCore" in the search box and click Install

Step: 3

Now, you have to go to the Startup.cs file, go to ConfigureServices, and AddSwaggerGen. 
The method will look like this.

  1. public void ConfigureServices(IServiceCollection services) {  
  2.     services.AddMvc();
  3.     services.AddSwaggerGen(c => {  
  4.         c.SwaggerDoc("v1"new Info {  
  5.             Version = "v1",  
  6.                 Title = "My API"   
  7.         });  
  8.     });  
  9. }  

After that, add the Swagger UI to the Configure method, after which it will look like this.

  1. public void Configure(IApplicationBuilder app, IHostingEnvironment env) {  
  2.     if (env.IsDevelopment()) {  
  3.         app.UseDeveloperExceptionPage();  
  4.     }  
  5.     app.UseMvc();  
  6.     app.UseSwagger();  
  7.     app.UseSwaggerUI(c => {  
  8.         c.SwaggerEndpoint("/swagger/v1/swagger.json""My API V1"); 
  9.     });  
  10. }  

Step: 4

  • In Solution Explorer, select a project.
  • On the Project menu, choose Properties.
  • Go to DebugLaunch browser> Set "swagger" in the text box.
  • Step: 5

      Run the project in Visual Studio 2017 and upper version.
      The output looks like:  

    Comments
    Read next