You might have heard that Swagger is removed in the templates of ASP.NET 9. So this blog post will show you alternatives.
Swagger removed
Swagger is a graphical tool that sits on top of the Open API which gives a graphical interface to interact and test your API. The team announced that they removed Swagger from the templates in ASP.NET 9. Mainly because the team wants to leverage their own implementation and don't want to depend on Swashbuckle, which, according to the thread, is not longer maintained.
If you spin up a new project, you will "only" find this:
builder.Services.AddOpenApi();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
AddOpenApi
and MapOpenApi
come from Microsoft.AspNetCore.OpenApi
which is now included by default. And with this, you can generate the Open API specification! If you spin up you app and go to https://localhost:<port>/openapi/v1.json
you have the spec!
But the interactivity is gone. So here are some options.
Just install Swashbuckle again!
You can just install Swashbuckle again. It's still maintained and works perfectly. Just install the package and add the following code to your Program.cs
:
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.3" />
And there we go! Swagger is back! The advantage of Swagger is, that it's really customizable and many folks need this degree of customization.
NSwag
Another option is NSwag. Also NSwag is capable of serving the Swagger UI. And similiar to Swashbuckle, you can also automatically create TypeScript types, ....
Scalar
A relatively new one is: Scalar. They have plenty of integration and of course also for .NET. I do like the appeal of their generated website. A quick onboarding guide is available here. But the very basic bit is:
Add the package:
dotnet add package Scalar.AspNetCore
And add the following code to your Program.cs
:
builder.Services.AddOpenApi();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
+ app.MapScalarApiReference();
}
Done! And this is how it looks: