liveBook · Manning

Swagger UI

As explained in Building Web APIs with ASP.NET Core

Swagger UI is a browser-based interface that displays OpenAPI descriptions and lets users interactively call documented HTTP APIs.

Swagger UI is a browser-based interface for exploring and calling a web API. In an ASP.NET Core project, Swashbuckle generates an OpenAPI description, usually served at /swagger/v1/swagger.json; Swagger UI reads that file and presents the endpoints, their return types, and interactive controls at /swagger. Controller and Minimal API endpoints added to the board-game API appear there automatically. section 3.2.2

// ...
 
builder.Services.AddSwaggerGen();    #1
 
// ...
 
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();                #2
    app.UseSwaggerUI();              #3
}

Register the Swagger generator with AddSwaggerGen, then enable the description endpoint and UI with UseSwagger and UseSwaggerUI. Here, both middleware calls run only in the Development environment. section 3.2.2

Run an endpoint without typing its URI

Open /swagger/index.html, expand PUT /Seed, select Try It Out, and click Execute. Swagger UI sends the PUT request and displays the response. If seeding fails with HTTP 500, inspect the response body: one failure revealed a SQL Server identity-column error. This makes the UI useful for testing requests that cannot be made by simply visiting a URL. section 5.3.6

Swagger UI can also help test authenticated requests. Configure an OpenAPI bearer security definition and a global security requirement in AddSwaggerGen. The resulting Authorize button accepts a JWT and places it in the Authorization HTTP header before requests are sent. This is a testing convenience; an application client would normally attach its token through an HTTP interceptor. section 9.3.1

For request DTOs, the Example Value and Schema tabs show sample JSON and field information. Response examples may need help when an action has multiple return types. Add [ProducesResponseType] attributes for the relevant types and status codes so the configured documentation can display them. Before sharing the UI with testers, check its environment-based availability separately from the developer exception page. section 11.2.6 section 2.5

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Shows middleware setup, endpoint execution, bearer-token configuration, and response annotations; defers broader Swashbuckle configuration.
Examples
Uses a PUT seeding request, an HTTP 500 error response, and account-endpoint response samples.
What it assumes
Starts from an ASP.NET Core web API template with OpenAPI support and identifies the service and middleware needed for Swagger UI.
What you can do after
Apply it via Swashbuckle to browse endpoints, execute requests, inspect responses, and supply a bearer token.
How it connects
Connects OpenAPI descriptions, Swashbuckle, HTTP endpoints, DTOs, JWT bearer authentication, and response-status annotations.

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 Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.