teociaps / SwaggerUI.Themes

Change theme to your Swagger API documentation in ASP.NET Core applications.
MIT License
11 stars 0 forks source link
aspnetcore dark-theme extension nswag swagger swagger-ui themes



SwaggerUI.Themes

Build Passing Tests Passing

Change style to your API documentation in ASP.NET Core applications!


Package Purpose
Swashbuckle Nuget Version Customize the style for Swashbuckle.AspNetCore.SwaggerUI
NSwag Nuget Version Customize the style for NSwag.AspNetCore

[!WARNING] Starting from v1.0.0 the namespace for pre-defined styles is AspNetCore.Swagger.Themes instead of AspNetCore.SwaggerUI.Themes!

Features

Supported .NET Versions

Version Status
.NET 6 Badge
.NET 7 Badge
.NET 8 Badge

Swashbuckle.AspNetCore.SwaggerUI

Customize the Swashbuckle API documentation UI by using AspNetCore.SwaggerUI.Themes in your ASP.NET Core project:

  1. Install the package using .NET CLI or NuGet Package Manager:

    dotnet add package AspNetCore.SwaggerUI.Themes

    or

    Install-Package AspNetCore.SwaggerUI.Themes
  2. In your Program.cs file, add the style through the Style, ModernStyle or NoJsModernStyle class as new parameter of app.UseSwaggerUI() method:

    using AspNetCore.Swagger.Themes;
    
    ...
    
    app.UseSwaggerUI(ModernStyle.Dark, options => ...);

    This code enables the chosen theme for Swagger UI in your application.

[!NOTE] Using the InjectStylesheet() method in the Swagger UI configuration will override the provided style. See here how to inject custom styles.

NSwag.AspNetCore

Customize the NSwag API documentation UI by using NSwag.AspNetCore.Themes in your ASP.NET Core project:

  1. Install the package using .NET CLI or NuGet Package Manager:

    dotnet add package NSwag.AspNetCore.Themes

    or

    Install-Package NSwag.AspNetCore.Themes
  2. In your Program.cs file, add the style through the Style, ModernStyle or NoJsModernStyle class as new parameter of app.UseSwaggerUi() method:

    using AspNetCore.Swagger.Themes;
    
    ...
    
    app.UseSwaggerUi(ModernStyle.Dark, settings => ...);

    This code enables the chosen theme for Swagger UI in your application.

[!NOTE] Setting the CustomInlineStyles property while configuring the NSwag settings will override the provided style. See here how to inject custom styles.

Available Themes

There are a few pre-defined styles available for your Swagger UI.

Classics:

Dark Forest DeepSea Desert
dark style example image forest style example image deepSea style example image desert style example image
Style.Dark
Style.Forest
Style.DeepSea
Style.Desert

The light style is not in this list because it's just the default one used by Swagger UI; to use that you don't need this library.

Moderns:

Light Dark Forest
modern light style example image modern dark style example image modern forest style example image
ModernStyle.Light
ModernStyle.Dark
ModernStyle.Forest
DeepSea Desert Futuristic
modern deepSea style example image modern desert style example image modern futuristic style example image
ModernStyle.DeepSea
ModernStyle.Desert
ModernStyle.Futuristic

[!TIP] Opt for Modern Styles! Modern styles come with additional functionalities, including pinned topbar and back-to-top button.

🆕You can also choose for a version without additional JavaScript if desired by using the pre-built NoJsModernStyle class.

[!NOTE] The classic and modern dark styles will only load if your browser's color scheme preference is set to dark; otherwise, the light style is loaded.

🆕Custom Styles

You can customize the Swagger UI in your ASP.NET Core application by applying custom CSS styles. Here are the available methods.

Inline CSS Styles

You can directly write your custom CSS content as a string and apply it to the Swagger UI:

var cssContent = "body { background-color: #f5f5f5; }";

// Swashbuckle
app.UseSwaggerUI(cssContent, options => ...);

// NSwag
app.UseSwaggerUi(cssContent, settings => ...);

Applying Embedded CSS Files from Assemblies

To apply a CSS file that is embedded as a resource within an assembly, place the file in a folder named "SwaggerThemes" inside the assembly. Then, specify the CSS filename and the assembly where the file is located:

var assembly = Assembly.GetExecutingAssembly();
var cssFileName = "myCustomStyle.css"; // Only the filename, no need to specify the "SwaggerThemes" folder

// Swashbuckle
app.UseSwaggerUI(assembly, cssFileName, options => ...);

// NSwag
app.UseSwaggerUi(assembly, cssFileName, settings => ...);

[!TIP] If your CSS file's name starts with "classic." or "modern.", the method automatically prepends a related common style (either classic or modern) to your custom styles. These common styles serve as the base for pre-defined styles that enhance the Swagger UI.

Creating Custom Styles by Inheriting from Base Classes

Another powerful customization option is to create your own style classes by inheriting from the Style, ModernStyle or NoJsModernStyle base classes. This approach allows you to define new styles that automatically incorporate common base styles and, for modern themes, additional JavaScript.

Here’s how to create a custom style:

// Use modern style loading additional JS
public class CustomModernStyle : ModernStyle
{
    protected CustomModernStyle(string fileName) : base(fileName)
    {
    }

    public static CustomModernStyle CustomModern => new("modern.custom.css");
}

// Use modern style without loading additional JS
public class CustomNoJsModernStyle : NoJsModernStyle
{
    protected CustomNoJsModernStyle(string fileName) : base(fileName)
    {
    }

    public static CustomNoJsModernStyle CustomModern => new("modern.custom.css");
}

// Use classic style
public class CustomStyle : Style
{
    protected CustomStyle(string fileName) : base(fileName)
    {
    }

    public static CustomStyle Custom => new("custom.css");
}

Place your CSS file (e.g., "custom.css") as an embedded resource in the same assembly where your custom style class is located. Then, apply it to the Swagger UI:

var customStyle = CustomStyle.Custom;

// Swashbuckle
app.UseSwaggerUI(customStyle, options => ...);

// NSwag
app.UseSwaggerUi(customStyle, settings => ...);

[!NOTE] Custom styles that inherit from the ModernStyle class include additional JavaScript. This doesn't apply to styles that inherit from NoJsModernStyle, inline styles, or those applied directly from assemblies.

Contributing

If you have any suggestions, bug reports, or contributions, feel free to open an issue or submit a pull request