Configure Swagger UI in Asp.net Core Web API

Documentation is very much important when Web.API has been exposed for public access. A clean documentation gives a more helpful to integrate APIs.Today, we are going to discuss and implement simple yet powerful API documentation library that's definitely going to help you to make your works easy.

What is Swagger?

Swagger provides rich documentation and seamless  API testing experience to developers. It is a open source framework backed by a large ecosystem of tools that helps you design, build, document, and consume your RESTful APIs. Swagger UI allows anyone — be it your development team or your end consumers — to visualize and interact with the API’s resources without having any of the implementation logic in place. It’s automatically generated from your Swagger specification, with the visual documentation making it easy for back end implementation and client side consumption.

Add Swagger to ASP.NET Core Web.API

Setup new ASP.NET Core Web.API project with visual studio default web API template.

Install swagger from nuget  using package manager console.
PM> Install-Package Swashbuckle -Pre
Here, 6.0.0-beta902 version will be installed in the project.

Configuration

Open Startup.cs class and locate a ConfigureServices method to register Swagger as a service.
public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc();
    services.AddSwaggerGen();
}

Once the service is configured, navigate to Configure method to add below codes. Here, app.UseSwaggerUi() will set up the dashboard http://<website-url>/Swagger/ui, and app.UseSwagger() will enable middleware to serve generated Swagger as a JSON endpoint.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    app.UseMvc();

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

After running the web API application, append the swagger ui url and you will be presented with a page similar to this.
swagger-in-aspnetcore-webapi-dotnetspan-img
The page contains list of the controllers and API methods available publicly and allows to execute different method straight through the browser. Here we have ValuesController along with it's all API methods. A different colors are used to identify different HttpVerb API methods. Expanding an API by clicking on it will gives you a unique testing experience.

Configure Swagger UI url

Be default Swagger set http://<website-url>/Swagger/ui to access the UI. It's easy to configure your custom url.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));    
    loggerFactory.AddDebug();

    app.UseMvc();

    app.UseSwagger();
    app.UseSwaggerUi("/Swagger/dashboard");
}
Swagger will set a new route and display UI on given route.

Reference