How to use Swagger in C#?

To use Swagger in C#, you need to install the Swashbuckle.AspNetCore library through the NuGet package manager. Here are the basic steps:

  1. Open Visual Studio and create an ASP.NET Core Web API project.
  2. Search for and install the Swashbuckle.AspNetCore library in the NuGet Package Manager.
  3. Add Swagger service configuration in the ConfigureServices method in the Startup.cs file.
services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
});
  1. Enable the Swagger middleware in the Configure method of the Startup.cs file.
app.UseSwagger();
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
  1. Simply start the project and access /swagger/index.html to view the generated API documentation.

Swagger allows you to easily view the definition, parameters, requests, and responses of APIs, and you can also perform interactive API testing using SwaggerUI.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds