Swagger

Swagger - Getting Started

Swagger provides an interactive API documentation UI based on OpenAPI.

1. Install Swagger in ASP.NET Core

In your Web API project, install the NuGet package:

dotnet add package Swashbuckle.AspNetCore

2. Configure Swagger in Program.cs

Enable Swagger middleware in your ASP.NET Core Web API:

// Program.cs
var builder = WebApplication.CreateBuilder(args);

// Add services
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Enable Swagger
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.MapControllers();
app.Run();
        

3. Run and Access Swagger UI

Start your API and navigate to the Swagger UI in your browser:

https://localhost:5001/swagger

4. Explore and Test Endpoints

Swagger lists all available endpoints with HTTP methods, parameters, and request/response schemas. You can test them directly from the UI.

5. Deploy with Swagger

Swagger UI is typically enabled in Development. To expose it in production, configure authentication and restrict access before enabling it.