Swagger Replacement in ASP.NET 9

9/23/2024
2 minute read

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: image

Create your own Validationattribute in ASP.NET Core

In this small blog post, I will show you how to create your own Validation attribute in ASP.NET Core to tailor-made your validation rules.

Generate http files from a swagger definition

Http files are nice and handy - but they are also a bit of a pain to update. So why not generate them from a swagger definition?

ASP.NET Core - Why async await is useful

Did you ever wonder why you "should" use async and await in your ASP.NET Core applications? Most probably, you heard something about performance. And there is some truth to it, but not in the way you might think.

So let's discuss this with smaller examples.

An error has occurred. This application may no longer respond until reloaded. Reload x