Introduction
With the release of .NET 9, you might have noticed that Swagger Gen is no longer included by default. But don't worry! Microsoft has introduced OpenAPI support along with a new modern API viewer called Scalar.
What's New in .NET 9?
.NET 9 now ships with OpenAPI support built-in, which offers a more modern and flexible way to document and explore your APIs. Scalar has also been introduced as a fresh, redesigned API viewer that is more intuitive and developer-friendly than the traditional Swagger Gen UI.
Prerequisites
- .NET 9 sdk (For visual studio code users)
- Upgrade Visual Studio to latest version for .NET 9 support
- Coffee (optional)
Adding Scalar/Swagger to Your .NET 9 Project
Here's how you can easily add Scalar to your .NET 9 project:
STEP 1:
Click on Create a new project
STEP 2:
On the search bar, search for web api
as we want to create a demo web api in .NET 9
STEP 3:
Now you need to configure your new project
STEP 4:
Select the target framework as .NET 9
.
Also select Configure for HTTPS
, Use Controllers
. Enlist in .NET Aspire orchestration
is optional.
STEP 5:
After your project is created, it will look something like
STEP 6:
Now in your service
project, go to Program.cs
file, it will look something like this by default.
STEP 7:
Now in order to add Scalar
, go to Nuget package manager
and install the Scalar.AspNetCore
package.
Alternatively, if you want to install Swagger
, then install Swashbuckle.AspNetCore
package.
STEP 8:
As I'm going with Scalar, so my code will look something like
builder.Services.AddOpenApi();
//some code
if(app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.MapScalarApiReference();
}
For swagger, it will look like,
builder.Services.AddSwaggerGen();
// some code
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
All Set!
As I used .NET Aspire orchestration
, I just need to click on run and Aspire
will take care of the rest!
Result:
Navigate to your-url:portnumber/scalar/v1
to view all the APIs.
Now this is the scalar interface and amazing part is that they provide customizable themes of different types, you can check them out.
This is my final response after hitting Run
button.
Conclusion
If you like this article and find it useful, dont forget to hit the like and follow button for more such upcoming articles. Also, if you face any issue feel free to discuss in the comment section. Happy coding!
Top comments (0)