liveBook · Manning

Swagger UI

As explained in .NET in Action, Second Edition

Swagger UI is a browser-based interface that renders an API description and allows users to send test requests to the API.

Swagger UI is a browser interface for exploring and testing an API. It displays endpoints and schemas from a Swagger/OpenAPI description, then lets you send requests without switching to curl or Postman. OpenAPI names the specification; Swagger remains a familiar name for its tooling. section 5.5

Listing 5.28 Adding Swagger to Program.cs
var builder = WebApplication.CreateBuilder(args);
  
builder.Services.AddDbContext<CatalogContext>();
builder.Services.AddControllers();
builder.Services.AddSwaggerGen();         #1
  
var app = builder.Build();
  
app.UseHttpsRedirection();
app.MapControllers();
  
app.UseSwagger();                         #1
app.UseSwaggerUI();                       #1
  
// ...
  
app.Run();

Add `Swashbuckle.AspNetCore`, then register Swagger generation with `AddSwaggerGen()` and enable the document and interface with `UseSwagger()` and `UseSwaggerUI()` in `Program.cs`. section 5.5

Run the application and open `https://localhost:<port>/swagger/index.html`. If `dotnet watch` does not pick up the configuration changes and the Swagger URLs fail, stop and restart the process. section 5.5

Try a controller request

Expand `GET /Catalog`, select **Try It Out**, enter any parameters, and execute the request to inspect the response. The same interface is useful when testing controller actions that create, update, or delete books with non-GET requests. section 5.5

Swagger UI can also authenticate with an identity provider and send bearer tokens to APIs. That capability is distinct from the basic setup and request-testing flow shown here. section 9.7

How .NET in Action, Second Edition covers it

How deep it goes
Walks through package installation, startup configuration, and a test request; mentions authentication capability without configuring it.
Examples
Uses `Program.cs` registration and a `GET /Catalog` request alongside book creation, update, and deletion endpoints.
What it assumes
Assumes an ASP.NET Core application with controllers while showing how to add and enable Swagger UI.
What you can do after
Apply it via Swashbuckle.AspNetCore to expose Swagger UI and test controller requests from a browser.
How it connects
Connects Swagger UI to OpenAPI, ASP.NET Core controllers, HTTP methods, identity providers, and bearer tokens.

Also explained in

Compare how 3 books explain Swagger UI →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.