Swagger (or OpenAPI) is a language-agnostic specification documenting your api. You can read more from there, ASP.NET Core web API documentation with Swagger / OpenAPI | Microsoft Docs
Generate a Asp.Net Core web api project with .NET CLI with this command,
dotnet new webapi -o YourApiProject
The template generated with this command already comes with swagger setup.
First you can verify that by going to YourApiProject.csproj config file, you will see the reference added,
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
How the swashbucke is setup you can go to this link, Get started with Swashbuckle and ASP.NET Core | Microsoft Docs
Now, go to the Startup.cs file, and in the configure method, you will see how the swagger ui is setup,
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
Now when you run your project with dotnet run, you will see the nice swagger ui in http://localhost:5001/swagger
To serve the Swagger UI at the app's root (http://localhost:<port>/), set the RoutePrefix property to an empty string:
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.RoutePrefix = string.Empty;
});
Swagger UI will look like this, which allow you to interact with your api.

Comments
Post a Comment