Well, modern way is to use Swashbuckle NuGet Package:
dotnet add package Swashbuckle.AspNetCore
It takes care about generating JSON file with documentation of your .NET API.
In code you need to make following adjustments:
builder.Services.AddSwaggerGen(options =>
{
// Optional: Include XML comments
var xmlFilename = $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml";
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
});
var app = builder.Build();
// Enable Swagger middleware
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.RoutePrefix = string.Empty; // Serve Swagger UI at app root
});
}
Now you are able to go to swagger page /swagger and access also underlaying JSON documentation file /swagger/v1/swagger.json.
Also, for documentation generation there is also Microsoft package that can be used for that. Please take a look at Generate OpenAPI documents
To use it you need nuget package
dotnet add package Microsoft.AspNetCore.OpenApi
with following changes in code:
builder.Services.AddOpenApi();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.UseSwaggerUI(options => options.SwaggerEndpoint("/openapi/v1.json", "Open API"))
}
Then, in swagger UI you need to add path to Open API docs
References:
Get started with Swashbuckle and ASP.NET Core