Updated October 2026 for .NET 10 (LTS) and Swashbuckle.AspNetCore 10.x.

You add builder.Services.AddSwaggerGen() to your ASP.NET Core API and the build fails because the method cannot be found. In almost every case the Swashbuckle package is missing or in the wrong project. In .NET 10 it is even more common, because new Web API projects no longer include Swashbuckle at all. Both fixes are below.

The error

error CS1061: 'IServiceCollection' does not contain a definition for 'AddSwaggerGen' and no accessible extension method 'AddSwaggerGen' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)

Why it happens

  • The Swashbuckle.AspNetCore package is not installed. AddSwaggerGen() is an extension method that comes from Swashbuckle, not from ASP.NET Core itself.
  • Your project was created from the .NET 10 Web API template. The template uses built-in OpenAPI support (AddOpenApi()) instead of Swashbuckle. Older tutorials that call AddSwaggerGen() fail in these projects until you add the package.
  • Only part of Swashbuckle is installed, for example just Swashbuckle.AspNetCore.SwaggerUI. AddSwaggerGen() lives in Swashbuckle.AspNetCore.SwaggerGen.
  • The package is in another project, such as a class library, instead of the API project where Program.cs calls it.

Fix 1: Install Swashbuckle.AspNetCore

Run this in the folder of your API project (the one with Program.cs):

dotnet add package Swashbuckle.AspNetCore

Or in Visual Studio’s Package Manager Console:

Install-Package Swashbuckle.AspNetCore

This one package brings in Swashbuckle.AspNetCore.Swagger, SwaggerGen and SwaggerUI, so you do not need to add them separately. Then register it in Program.cs:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer(); // needed for minimal APIs
builder.Services.AddSwaggerGen();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();    // serves /swagger/v1/swagger.json
    app.UseSwaggerUI();  // serves the UI at /swagger
}

app.MapControllers();
app.Run();

No extra using line is needed: AddSwaggerGen() is in the Microsoft.Extensions.DependencyInjection namespace, which is already available. Build again and the error is gone.

Fix 2: Use built-in OpenAPI (the .NET 10 default)

You may not need AddSwaggerGen() at all. .NET 10 generates the OpenAPI document itself with the Microsoft.AspNetCore.OpenApi package, which the Web API template already includes:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddOpenApi();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.MapOpenApi(); // serves /openapi/v1.json
}

app.MapControllers();
app.Run();

The built-in version produces the JSON document but no UI. To keep the familiar Swagger UI, add only the UI package and point it at the new document:

dotnet add package Swashbuckle.AspNetCore.SwaggerUI
if (app.Environment.IsDevelopment())
{
    app.MapOpenApi();
    app.UseSwaggerUI(options =>
        options.SwaggerEndpoint("/openapi/v1.json", "My API v1"));
}

Which should you choose? For new .NET 10 projects, built-in OpenAPI is Microsoft’s default. Keep Swashbuckle (Fix 1) if you have existing filters, examples or IOperationFilter / IDocumentFilter code you do not want to rewrite.

Upgrading to Swashbuckle 10? Watch for these changes

Swashbuckle 10 uses version 2 of the Microsoft.OpenApi library. Basic setup like the code above still works, but custom code may need updates:

  • Add using Microsoft.OpenApi; in files that customise schemas or operations. Interfaces such as IOpenApiSchema now live there.
  • Schema filters receive IOpenApiSchema. Cast to OpenApiSchema before changing properties.
  • schema.Type = "string" becomes schema.Type = JsonSchemaType.String, and Nullable = true becomes schema.Type |= JsonSchemaType.Null.
  • AddSecurityRequirement() now takes a function that receives the OpenApiDocument. Check this if you add JWT authorization to Swagger.

Still not working? Quick checklist

  • Run dotnet list package in the API project and confirm Swashbuckle.AspNetCore (or SwaggerGen) is listed.
  • Make sure all Swashbuckle packages use the same version. Mixing major versions causes missing-method errors.
  • Run dotnet restore, then clean and rebuild. In Visual Studio, closing and reopening the solution clears stale IntelliSense.
  • Check <TargetFramework> in the .csproj is net10.0.

FAQ

Is Swagger removed from .NET?

No. Only the template changed. Swashbuckle is still maintained and works with .NET 10. New projects simply start with Microsoft’s built-in OpenAPI document instead.

Where is my swagger.json now?

With Swashbuckle it is at /swagger/v1/swagger.json. With built-in OpenAPI it is at /openapi/v1.json. See how to export Swagger JSON or YAML.

Swagger UI loads but says “Failed to load API definition”

That is a different problem, usually a route or schema error in one of your controllers. Follow Failed to load API definition – fixes.

Related

Sources: NuGet: Swashbuckle.AspNetCore · Microsoft Learn: OpenAPI support in ASP.NET Core · Swashbuckle on GitHub

Leave a Reply

Your email address will not be published. Required fields are marked *