Adding Swagger to my ASP.Net Core API

Continuing on the MyReverie solution building upon my last post on this My Reverie.

What I would now like to do is add some API documentation around the my API

The reason I am choosing swagger is simply that it is the largest framework of API developer tools for the OpenAPI specific which allows rapid documentation and testing my APIs with great ease.

For reference check out their website @ https://swagger.io/

Let’s get started

First we want to add the Swashbuckle package from Nuget

So right click the Goals.API project

Click Manage Nuget Packages

Nuget

Under Browse tab search for Swashbuckle.AspNetCore

Package

Once found click install

Once installed we want to add the Swashbuckle namespace ‘Swashbuckle.AspNetCore.Swagger’ to our Startup class

In the ConfigureServices method add:

services.AddSwaggerGen(c =>

{

c.SwaggerDoc(“v1”, new Info { Title = “My Reverie”, Version = “v1” });

});

ConfigureServices

Next, In the Configure method

In the cope of the env.IsDevelopment() check, add

app.UseSwagger();

app.UseSwaggerUI(options =>

{

options.SwaggerEndpoint(“/swagger/v1/swagger.json”, “My Reverie V1”);

});

Configure

Lets run it to see what it looks like, you should see something like below

image

To try it out simple select the Get button

Click the Try it out button on top right

Then hit execute

image

Summary:
We’ve just added swagger to the API to add a basic documentation and testing base in which to build upon.