Setup Swagger core

What is Swagger?

Swagger is an open-source framework which can automatically generate an interactive document for your API in a human and machine-readable way.


  1. Save time in creating a spec for your API
  2. The API spec always has the latest changes updated
  3. Makes it easier for QA to test the API

How to Setup Swagger in ASP.NET core 2.0 API

First of all, I need to have an API project to show how we can setup the Swagger; therefore, I am going to create a basic ASP.NET Core API project.

Create an ASP.NET Core API

I am going to create a ASP.NET Core Web Application using Visual Studio 2017, and I will name it TestApi.

Choose Web API template, and make sure you chose the ASP.NET Core 2.0 from the drop-down list.

The API is ready and now we can start setting up the Swagger.

Adding Swagger package to the API Project

  1.  Open Package Manager Console from Tools > NuGet Package Manager > Package Manager Console .
  2.  Type Install-Package Swashbuckle.AspNetCore , then wait till the package get installed.

Setup Swagger in Startup.cs

Now, you can open the Startup.cs file and setup Swagger there. You can find the changes in lines 29 – 32 and 43 – 48.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Swashbuckle.AspNetCore.Swagger;

namespace TestApi {
  public class Startup {
    public Startup (IConfiguration configuration) {
      Configuration = configuration;

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices (IServiceCollection services) {
      services.AddMvc ();

      services.AddSwaggerGen (c => {
        c.SwaggerDoc ("v1", new Info { Title = "Test API", Version = "v1" });

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure (IApplicationBuilder app, IHostingEnvironment env) {
      if (env.IsDevelopment ()) {
        app.UseDeveloperExceptionPage ();

      app.UseSwagger ();

      app.UseSwaggerUI (c => {
        c.SwaggerEndpoint ("/swagger/v1/swagger.json", "Test API V1");

      app.UseMvc ();


Don’t forget to write your application name instead of the word “test” that I used as my app name.

After adding the configurations you can run the project and direct to /swagger where you can see the Swagger generated pages and you can also test against your endpoints.