Sunday, March 1, 2020

Adding swagger to your ASP.NET Core Project

1) Installation
Install the Nuget Package "Swashbuckle.AspNetCore" to your project.







2) Add swagger services to middleware

Add the following code in the startup.cs classes ConfigureServices method

public void ConfigureServices(IServiceCollection services)
       {
services.AddSwaggerGen( c => {
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
});
                }

3) Enable swagger

In the configure method add the below code

            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
           {
                        //Enable to serve swagger as a JSON end point
app.UseSwagger();
                        //Enable to serve the swagger UI
app.UseSwaggerUI(c => {
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
            }

4) Configure the Debug launch
In the project properties, Debug section under the "Launch browser" add the URL
Now when you start debugging the browser will open up the swagger for you.



No comments:

Post a Comment