...

/

Understanding and Testing Swagger

Understanding and Testing Swagger

Learn about enabling Swagger for a web service and testing HTTP requests for various scenarios using Swagger UI.

The most important part of Swagger is the OpenAPI Specification, which defines a REST-style contract for the API, detailing all its resources and operations in a human- and machine-readable format for easy development, discovery, and integration.

Developers can use the OpenAPI Specification for a Web API to automatically generate strongly typed client-side code in their preferred language or library. For us, another useful feature is Swagger UI because it automatically generates documentation for the API with built-in visual testing capabilities.

Swagger with Swashbuckle

Let’s review how Swagger is enabled for our web service using the Swashbuckle package:

Step 1: If the web service is running, shut down the web server.

Step 2: In Northwind.WebApi.csproj, note the package reference for Swashbuckle.AspNetCore, as shown in the following markup:

Press + to interact
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>

Step 3: In Program.cs, import Swashbuckle’s SwaggerUI namespace, as shown in the following code:

Press + to interact
using Swashbuckle.AspNetCore.SwaggerUI; // SubmitMethod

Step 4: In the section configuring the HTTP request pipeline, note the statements to use Swagger and ...