Menu

Web API discoverability using Swagger

With many web APIs laying around, no one can possibly remember all the endpoints, parameters and return data.

This is where the Web API Help page comes into picture, otherwise you will just have to open the Web API project, whenever you need to find more information about a particular endpoint, That is just a waste of time.  You can use Swagger for this there is a nice nuget called “Swashbuckle” by Richard Morris – Thank you.

Install-Package Swashbuckle -Pre

open your Startup.cs and add below to your ConfigureServices section

public void ConfigureServices(IServiceCollection services)
{
   ......
   ......
   services.AddMvc();

   services.AddSwaggerGen();
 }

and add below to your Configure section

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ExchangeMonitorContext context)
{
    .....
    app.UseStaticFiles();

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });

    app.UseSwagger();
    app.UseSwaggerUi();
   
    .....
}

And API documentation will be generated on http://localhost:49875/swagger/ui/index.html – Sweet.

 

 

Leave a comment