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.

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.

services.AddSwaggerGen(c =>

{

c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });

});

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

[HttpGet]

public ActionResult<IEnumerable<string>> Get()

{

returnnewstring[] { "value1", "value2" };

}

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

app.UseSwagger();

app.UseSwaggerUI(c =>

{

c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");

});

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

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.

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