Swagger UI Dark Theme for .NET: Drop-in CSS Dark Mode (Step by Step)
Switch Swagger UI to dark mode in .NET Core and FastEndpoints with a drop-in CSS theme — step-by-step setup, no rebuild of your API docs required.
The default Swagger UI is bright white, and if you keep it open next to a dark IDE all day it can blind you. There is no built-in dark mode switch, but the UI is plain HTML and CSS, so one stylesheet fixes it. This post walks through adding a dark theme in the three setups you are most likely to have: Swashbuckle, NSwag (including FastEndpoints), and the .NET 9+ templates that no longer ship Swagger at all.
The stylesheet we use is SwaggerDark.css on GitHub. Big shoutout to the original blog post: Turn Swagger Theme to the Dark Mode - DEV Community.
Step 1: Add the stylesheet to your project
Download SwaggerDark.css and save it under wwwroot, for example wwwroot/swagger-ui/SwaggerDark.css. Check the repository’s license before you ship it in a commercial product.
Then make sure the app serves static files, before the Swagger middleware:
app.UseStaticFiles();
Step 2a: Swashbuckle (Swashbuckle.AspNetCore)
Swashbuckle’s UI options can inject an extra stylesheet into the Swagger UI page:
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.InjectStylesheet("/swagger-ui/SwaggerDark.css");
});
}
Reload /swagger and the dark theme is applied. Nothing about your OpenAPI document changes; only the page that renders it does.
Step 2b: NSwag and FastEndpoints
FastEndpoints uses NSwag for its Swagger UI, and NSwag can take the CSS inline. Read the file once at startup and hand it to the UI settings:
var swaggerDark = File.ReadAllText(
Path.Combine(app.Environment.WebRootPath, "swagger-ui", "SwaggerDark.css"));
application.UseSwaggerUi(config => config.TransformToExternalPath = (internalUiRoute, request) =>
{
config.CustomInlineStyles = swaggerDark;
return internalUiRoute;
});
Inline styles avoid a second request and don’t depend on static-file routing, which helps when the API sits behind a path prefix.
Step 2c: .NET 9 and later templates
Since .NET 9, the webapi template no longer includes Swashbuckle. It generates the OpenAPI document with Microsoft.AspNetCore.OpenApi (builder.Services.AddOpenApi() and app.MapOpenApi(), served at /openapi/v1.json) and ships no UI. If you still want Swagger UI, add only the UI package, Swashbuckle.AspNetCore.SwaggerUI, point it at that document, and inject the stylesheet the same way:
app.MapOpenApi();
if (app.Environment.IsDevelopment())
{
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/openapi/v1.json", "v1");
options.InjectStylesheet("/swagger-ui/SwaggerDark.css");
});
}
Alternatively, UIs such as Scalar come with a dark mode of their own, so if you’re choosing a docs UI from scratch on .NET 9 or 10, that’s worth comparing before you add a CSS override.
Troubleshooting
- The page is still white. Open the browser’s network tab and check that the CSS request returns 200. A 404 usually means
UseStaticFiles()runs after the Swagger middleware, or the file isn’t underwwwroot. - Some panels stay light. The stylesheet targets specific Swagger UI class names, and new Swagger UI versions occasionally rename them. Update the CSS from the repository, or add an override for the element you can see in dev tools.
- It works locally but not behind a reverse proxy. Use a path relative to the proxy prefix in
InjectStylesheet, or switch to inline styles as in the NSwag example.
Happy Coding!
Related Posts
Azure DevOps Pipelines for .NET 10 on a Single Server: Blue-Green Containers and an Agent on the Box
Our real Azure DevOps YAML for .NET 10 on one Linux server: an agent on the box, Docker images, blue-green swaps with rollback and gated migrations.
Duende IdentityServer 8 Example: Server, API and MVC Client on .NET 10 (Real Code)
A working Duende IdentityServer 8 example on .NET 10: EF stores, an MVC code-flow client and a JWT-protected API, quoted from a public repo you can run.
The N+1 Query Problem in EF Core: Benchmarking Every Fix on PostgreSQL
Actual BenchmarkDotNet numbers for N+1 vs Include, split queries, projections, and compiled queries in EF Core 10 on PostgreSQL 16.