Adding Swagger to C# API Projects
Since Dotnet 9.0 was released, Swagger no longer comes with the webapi project and needs to be installed manually instead.
Problem
Since Dotnet 9.0 was released, Swagger no longer comes with the webapi project and needs to be installed manually instead. Below are the steps to do this.
Solution
1) Add the package to your dotnet project by running the below in the Command Line:
1
dotnet add package Swashbuckle.AspNetCore
2) In the Program.cs file, add these two lines before builder.Build();
1
2
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
3) Still in the Program.cs file, add after var app = builder.Build();
1
2
3
4
5
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
4) On any endpoint you want viewable in Swagger should use WithOpenApi(). For example:
1
2
app.MapGet("/example", () => "Hello)
.WithOpenApi();
And that is it! Now when you run your Dotnet project in dev you can navigate to https://localhost:
Would you like to learn more?
See the Microsoft Documentation here.
Personal Note
I have been struggling to think of things to write about hence skipping a few months but I have been working on a lot of C# projects recently so the next few posts will probably be focused around this.