DEV Community

Cover image for No Swagger in .NET 9? Here's What You Need to Know!
Aditya
Aditya

Posted on

6 1 1 1 1

No Swagger in .NET 9? Here's What You Need to Know!

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

Create new project

STEP 2:

On the search bar, search for web api as we want to create a demo web api in .NET 9

Create new project

STEP 3:

Now you need to configure your new project

Configure

STEP 4:

Select the target framework as .NET 9.
Also select Configure for HTTPS, Use Controllers. Enlist in .NET Aspire orchestration is optional.

API setup

STEP 5:

After your project is created, it will look something like

Landing

STEP 6:

Now in your service project, go to Program.cs file, it will look something like this by default.

Program.cs

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.AspNetCorepackage.

Add 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();
}
Enter fullscreen mode Exit fullscreen mode

For swagger, it will look like,

builder.Services.AddSwaggerGen();

// some code

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
Enter fullscreen mode Exit fullscreen mode

Scalar API reference

All Set!

As I used .NET Aspire orchestration, I just need to click on run and Aspire will take care of the rest!

Aspire.net

Result:

Navigate to your-url:portnumber/scalar/v1 to view all the APIs.

URL

Now this is the scalar interface and amazing part is that they provide customizable themes of different types, you can check them out.

Scalar interface

This is my final response after hitting Run button.

API response

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!

Happy-coding

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay