Configure annotations in Swagger documentation for asp.net core api

.NET Core PROGRAMMING

Swashbuckle proposes some useful annotations to enrich documentation. This article describes how to enable annotations.

Prerequisites

How to configure swagger for asp.net core API?

View at Medium.com

Getting Started

Step 1: Install below package:

Swashbuckle.AspNetCore.Annotations

Step 2: Add below code inside ConfigureServices function in startup.cs file

services.AddSwaggerGen(c =>{c.EnableAnnotations();});
CONFIGURESERVICES FUNCTION INSIDE STARTUP.CS FILE

How to add Operation Annotation

SwaggerOperation is a useful attribute where you can set the summary, description, id, and the tags of an individual request/route.

Example, how to set summary & description is shown below:

SETTING SUMMARY & DESCRIPTION USING SWAGGEROPERATION ANNOTATION

Below image shows how summary and description will be displayed in swagger UI documentation:

SUMMARY & DESCRIPTION DISPLAYED USING SWAGGEROPERATION

How to add SwaggerResponse annotation:

SwaggerResponse is an attribute where you can set for each status code a description.

Example, how to set SwaggerResponse for different HTTP status codes is shown below:

Below image shows how SwaggerResponse for different status codes will be displayed in swagger UI documentation:

SWAGGER RESPONSE FOR DIFF. STATUS CODES

Thank you for reading. Keep visiting and share this in your network. Please put your thoughts and feedback in the comments section.

Follow me on LinkedIn Instagram Facebook Twitter

Leave a comment