Swagger Not Opening by Default in .NET 9 Web API – Why It Happens and How to Fix It

If you’ve recently created a .NET 9 Web API project and noticed that Swagger UI is not opening by default, you’re not alone.

Many developers upgrading from earlier versions of ASP.NET Core are surprised by this behavior.

In this blog post, we’ll cover:

  • What changed in .NET 9 regarding Swagger

  • Why Swagger is no longer enabled by default

  • Step-by-step instructions to re-enable Swagger

  • Common mistakes and best practices

  • An interview-ready explanation

The Problem Occurs when creating API project in .NET 9

After creating a new .NET 9 Web API project, you may experience:

  • Swagger UI not opening automatically

  • /swagger returning 404 Not Found

  • No Swagger configuration found in Program.cs

  • The application runs, but without API documentation

This happens even in Development mode, which can be confusing if you’re used to older .NET versions.

Why Swagger Is Missing in .NET 9

Starting with .NET 9, Microsoft removed Swagger from the default Web API project template.

Why did Microsoft do this?

  • To reduce template bloat

  • To avoid adding unused dependencies

  • To improve startup performance

  • To encourage explicit configuration rather than hidden defaults

Swagger is now considered an optional development tool, not something every API needs out of the box.

How to Enable Swagger in .NET 9 (Step-by-Step)

The fix is straightforward. You just need to add Swagger explicitly.

Step 1: Install Swagger NuGet Package

Run the following command in your project directory:

dotnet add package Swashbuckle.AspNetCore

This package provides:

  • Swagger document generation

  • Swagger UI

Step 2: Configure Swagger in Program.cs

Open Program.cs and update it as shown below:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container
builder.Services.AddControllers();

// Add Swagger services
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Enable Swagger only in Development environment
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

Step 3: Run the Application

Start your application:

dotnet run

Then open your browser and navigate to:

<https://localhost>:<port>/swagger

Swagger UI should now be visible and fully functional.

Optional: Open Swagger Automatically on Launch

If you want Swagger to open automatically when the app starts:

Visual Studio Users

  1. Open launchSettings.json

  2. Set the launchUrl to swagger

"launchBrowser": true,
"launchUrl": "swagger"

Common Mistakes Developers Make

  • Forgetting to install Swashbuckle.AspNetCore

  • Missing AddEndpointsApiExplorer()

  • Expecting Swagger to be available by default (old habit)

  • Enabling Swagger in Production unintentionally

Best Practices for Swagger in .NET APIs

  • Enable Swagger only in Development

  • Secure Swagger endpoints in Production

  • Customize Swagger for API versioning

  • Use Swagger as official API documentation, not just a testing tool

Conclusion

.NET 9 introduces a more minimal and intentional project template. While Swagger is no longer enabled by default, re-adding it takes only a few lines of code and gives you full control over when and how it’s used. Swagger remains an essential tool for API documentation ,Endpoint testing and Improves developer experience .

I hope this article helped you clearly understand why Swagger is missing in .NET 9 and how to resolve it effectively.