Introduction:
In the realm of modern APIs, the provision of clear and comprehensive documentation plays a pivotal role in facilitating developer adoption and ensuring efficient utilization. Swagger, aligned with the OpenAPI Initiative, stands out as a prominent solution, offering machine-readable documentation and a user-friendly interactive interface. In this guide, we'll delve into the seamless integration of Swagger into your .NET Core API.
Step 1: Install the necessary packages
Add Swashbuckle.AspNetCore NuGet package to a project:
dotnet add package Swashbuckle.AspNetCore
Add Swashbuckle.AspNetCore.SwaggerUI NuGet package to a project:
dotnet add package Swashbuckle.AspNetCore.SwaggerUI
Step 2: Add services in program.cs
In the program.cs file, include the following service additions:
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
Additionally, add middleware in program.cs to enable Swagger in the development environment:
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
Step 3: Run the API project and access the Swagger UI at:
https://your-api-base-url/swagger
Ensure the API project is running, and navigate to the provided URL to explore and interact with the Swagger UI seamlessly.
Step 3: Execute the APIs and test.
Experienced Software Developer at MagnusMinds IT Solution, specializing in .NET technologies including Framework, Core, MVC, ASP.NET, and Entity Framework with over 1 year of contribution.