liveBook · Manning

Swashbuckle.AspNetCore.Annotations

As explained in Building Web APIs with ASP.NET Core

A Swashbuckle annotation is an attribute-based mechanism for enriching generated OpenAPI documentation for an ASP.NET Core API.

Swashbuckle.AspNetCore.Annotations is an optional NuGet module that lets ASP.NET Core developers enrich generated OpenAPI documentation with attributes placed directly on endpoints and parameters. section 11.2.2 section 11.2.4

A practical setup follows these steps:

  • Install the Swashbuckle.AspNetCore.Annotations package through Visual Studio, the Package Manager Console, or the .NET CLI. section 11.2.4
  • Add [SwaggerOperation] to controller actions or Minimal API methods to define custom summaries, descriptions, and tags. section 11.2.4
  • Place [SwaggerParameter] directly above the input parameter it describes. section 11.2.4
  • Enable annotation processing globally with options.EnableAnnotations() inside AddSwaggerGen. section 11.2.4

Board-game API documentation

A BoardGamesController can describe its list endpoint with the summary “Get a list of board games” and explain its paging, sorting, and filtering behavior. Its query parameter can receive a [SwaggerParameter] description identifying the DTO used to customize retrieval. section 11.2.4

Listing 11.1 /Controllers/BoardGamesControllers.cs file: Adding annotations
using Swashbuckle.AspNetCore.Annotations;                             #1
 
// ... existing code
 
[HttpGet(Name = "GetBoardGames")]
[ResponseCache(CacheProfileName = "Any-60")]
[SwaggerOperation(                        #2
Summary = "Get a list of board games.",                    #3
Description = "Retrieves a list of board games " + #4
"with custom paging, sorting, and filtering rules.")]
public async Task<RestDTO<BoardGame[]>> Get(
 
// ... existing code
 
[HttpGet("{id}")]
[ResponseCache(CacheProfileName = "Any-60")]
[SwaggerOperation(#2
Summary = "Get a single board game.",                    #3
Description = "Retrieves a single board game with the given Id.")]#4
public async Task<RestDTO<BoardGame?>> Get(int id)
 
// ... existing code
 
[Authorize(Roles = RoleNames.Moderator)]
[HttpPost(Name = "UpdateBoardGame")]
[ResponseCache(CacheProfileName = "NoCache")]
[SwaggerOperation(#2
Summary = "Updates a board game.",                    #3
Description = "Updates the board game's data.")]#4
public async Task<RestDTO<BoardGame?>> Post(BoardGameDTO model)
 
// ... existing code
 
[Authorize(Roles = RoleNames.Administrator)]
[HttpDelete(Name = "DeleteBoardGame")]
[ResponseCache(CacheProfileName = "NoCache")]
[SwaggerOperation(#2
Summary = "Deletes a board game.",                    #3
Description = "Deletes a board game from the database.")]#4
public async Task<RestDTO<BoardGame?>> Delete(int id)
 
// ... existing code

Inspect the complete controller example, including annotations on multiple board-game operations. section 11.2.4

builder.Services.AddSwaggerGen(options =>
{
options.EnableAnnotations();  #1
 
    // ... existing code

Inspect the required global AddSwaggerGen configuration. section 11.2.4

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains installation, operation attributes, parameter attributes, and global enablement without detailing generated-schema internals.
Examples
Uses BoardGamesController operations, board-game query parameters, and authenticated Minimal API test endpoints.
What it assumes
Assumes an ASP.NET Core API with controller or Minimal API endpoints and basic familiarity with .NET package installation.
What you can do after
Apply it via Swashbuckle attributes and AddSwaggerGen configuration to produce clearer endpoint and parameter documentation.
How it connects
Connects OpenAPI documentation with XML documentation, controller actions, Minimal APIs, operation descriptions, and input parameters.

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.