· Adam Ferguson  · 2 min read

Enhance Your .NET 9 Web API with Scalar UI & Swagger's Robustness

Combine the best of both worlds! Use Swagger for reliable OpenAPI generation and elevate your API documentation with Scalar UI's stunning interface in .NET 9.

Do you want clean API documentation for your .NET 9 Web API, but hesitant to rely on newer, less mature OpenAPI generators? You don’t have to choose! You can leverage the robustness of Swagger (Swashbuckle.AspNetCore) for OpenAPI generation and the stunning visuals of Scalar UI.

This post shows you how, drawing from a complete example with extension methods for a streamlined setup.

Why Combine Swagger and Scalar UI?

While .NET 9 introduces options for generating OpenAPI documents, many developers still trust the maturity and stability of Swagger (via Swashbuckle.AspNetCore). Scalar UI, on the other hand, provides a more modern and interactive user interface compared to the standard Swagger UI.

Key Steps

1. Project Setup (Existing .NET 9 Web API)

This assumes you have a .NET 9 Web API project with Swashbuckle.AspNetCore already configured for swagger.json generation. If not, checkout the GitHub repository for the complete example.

2. Install Scalar.AspNetCore

Add the Scalar.AspNetCore NuGet package:

Terminal window
dotnet add package Scalar.AspNetCore

3. Configure with extension methods

using Microsoft.OpenApi.Models;
using Scalar.AspNetCore;
namespace YourNamespace;
public static class OpenApiConfigurationExtensions
{
public static void AddOpenApiConfiguration(this IServiceCollection services)
{
services.AddOpenApi();
services.AddEndpointsApiExplorer();
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "v1 Penguins API",
Version = "v1",
Description = "An example ASP.NET Core Web API using Swagger with Scalar UI",
});
});
}
public static void AddScalarUiConfiguration(this WebApplication app)
{
if (!app.Environment.IsDevelopment()) return;
app.UseSwagger(); // Existing Swagger
app.MapOpenApi().CacheOutput();
app.MapScalarApiReference(options =>
{
options
.WithTitle("v1 Penguins API")
.WithTheme(ScalarTheme.DeepSpace)
.WithDefaultHttpClient(ScalarTarget.CSharp, ScalarClient.HttpClient)
.WithOpenApiRoutePattern("/swagger/v1/swagger.json"); // Point to the generated swagger.json
options.Servers ??= new List<ScalarServer>();
options.Servers.Add(new ScalarServer("http://localhost:5063")); //Adjust as needed
});
}
}

4. Call extension methods in Program.cs

using YourNameSpace;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddOpenApiConfiguration(); // Call this extension method
var app = builder.Build();
app.AddScalarUiConfiguration(); // Call this extension method
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

5. Accessing Scalar UI

Run your .NET 9 Web API and navigate to the Scalar UI at http://localhost:<port>/scalar/v1.

Benefits of this approach

  • Swagger’s Reliable Generation: You continue to use Swashbuckle.AspNetCore, ensuring that your OpenAPI specification is generated with a mature and widely tested library.

  • Scalar UI’s Enhanced Interface: Your API documentation gets a significant visual and interactive upgrade with Scalar UI.

  • Clean Configuration: Extension methods make the setup process cleaner and more organized

View a complete example with source code on Github

Back to Blog

Related Posts

View All Posts »
Creating and publishing a .NET NuGet package

Creating and publishing a .NET NuGet package

Learn how to build a .NET library, pack for distribution, and seamlessly publish to your choice of public or private NuGet feeds. Learn how to leverage the power of GitHub Actions and Git tags to automate your workflow, ensuring efficient and reliable deployment of your .NET packages

Implementing Multi-tenancy in SaaS

Implementing Multi-tenancy in SaaS

Discover how to implement robust multi-tenancy in your SaaS application using .NET 8 Web API, Entity Framework Core, and PostgreSQL, ensuring secure data isolation and scalable architecture for cloud-based services

Inspiration behind Alertu

Inspiration behind Alertu

Alertu was born from firsthand experience with the challenges of cloud monitoring at major enterprises like ASOS, where the flood of email alerts and resulting alert fatigue inspired the creation of a streamlined, centralized solution for more effective cloud infrastructure management