ASP.NET Core API Documentation Using Swashbuckle.AspNetCore And .NET CLI

In this article, we can discuss how we can create documentation for API project. Swashbuckle.AspNetCore is a swagger tool for API’s built with ASP.NET Core. We shall create API project and add the documentation using Swashbuckle.AspNetCore project.

In this article, we will discuss how we can create documentation for API projects.

Swashbuckle.AspNetCore is a swagger tool for API’s built with ASP.NET Core. We shall create an API project and add the documentation using Swashbuckle.AspNetCore project.

For this, let's create an API project with the help of dotnet CLI. (To start building .NET apps you just need to download and install the .NET SDK (Software Development Kit). )

Create an API project under a new folder (example, APIDocumentation) using the following command in the command prompt:

dotnet new api

ASP.NET Core API Documentation Using Swashbuckle.AspNetCore And .NET CLI

Once we have run the commands, our scaffolds get crated with a sample controller called ValueController.

  1. namespace APIDocumentation.Controllers  
  2. {  
  3.     [Route("api/[controller]")]  
  4.     [ApiController]  
  5.     public class ValuesController : ControllerBase  
  6.     {  
  7.         // GET api/values  
  8.         [HttpGet]  
  9.         public ActionResult<IEnumerable<string>> Get()  
  10.         {  
  11.             return new string[] { "value1""value2" };  
  12.         }  
  13.   
  14.         // GET api/values/5  
  15.         [HttpGet("{id}")]  
  16.         public ActionResult<string> Get(int id)  
  17.         {  
  18.             return "value";  
  19.         }  
  20.   
  21.         // POST api/values  
  22.         [HttpPost]  
  23.         public void Post([FromBody] string value)  
  24.         {  
  25.         }  
  26.   
  27.         // PUT api/values/5  
  28.         [HttpPut("{id}")]  
  29.         public void Put(int id, [FromBody] string value)  
  30.         {  
  31.         }  
  32.   
  33.         // DELETE api/values/5  
  34.         [HttpDelete("{id}")]  
  35.         public void Delete(int id)  
  36.         {  
  37.         }  
  38.     }  
  39. }  

Install the standard package of Swashbuckle.AspNetCore into your ASP.NET Core application.

dotnet add package Swashbuckle.AspNetCore

 ASP.NET Core API Documentation Using Swashbuckle.AspNetCore And .NET CLI

Restore package dependencies using the following command

dotnet restore

ASP.NET Core API Documentation Using Swashbuckle.AspNetCore And .NET CLI

Once we have run the restore command, the CLI will install all the project dependencies under our directory.

In the next step, we must register our swagger generator in the configuration service. For that, we need to add the following code in startup.cs under ConfigureServices and add using Swashbuckle.AspNetCore.Swagger; in directives.

  1. services.AddSwaggerGen(c =>  
  2.             {  
  3.                 c.SwaggerDoc("v1"new Info { Title = "My API", Version = "v1" });  
  4.             });  

By default, your API actions and non-route parameters are decorated with explicit "Http" and "From" bindings. If it’s not available, we must add those bindings in our API.

Example

  1. [HttpGet]  
  2. public ActionResult<IEnumerable<string>> Get()  
  3. {  
  4.     return new string[] { "value1""value2" };  
  5. }  

Next, in the Configure method, we must insert middleware to expose the generated Swagger as JSON endpoint(s) like below,

  1. app.UseSwagger();  
  2.             app.UseSwaggerUI(c =>  
  3.             {  
  4.                 c.SwaggerEndpoint("/swagger/v1/swagger.json""My API V1");  
  5.             });  

We have completed all the steps that we need to set up a basic API documentation of swagger. Now we are able to run the application and visit the documentation.

We can run the application using the following CLI command

dotnet run

 ASP.NET Core API Documentation Using Swashbuckle.AspNetCore And .NET CLI

The application will start running and the endpoint information will get displayed in the command prompt.

We shall visit the documentation page using the link,

<<endpoint>>/swagger

Example

Here, our application is hosted in localhost:5001. So we can visit https://localhost:5001/swagger to visit our API documentation.

 ASP.NET Core API Documentation Using Swashbuckle.AspNetCore And .NET CLI ASP.NET Core API Documentation Using Swashbuckle.AspNetCore And .NET CLI

Based on our Controller and Http methods, our documentation has been generated. We can extend documentation further based on our needs.