WebSpark.Bootswatch 1.0.7

There is a newer version of this package available.
See the version list below for details.
dotnet add package WebSpark.Bootswatch --version 1.0.7
                    
NuGet\Install-Package WebSpark.Bootswatch -Version 1.0.7
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="WebSpark.Bootswatch" Version="1.0.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="WebSpark.Bootswatch" Version="1.0.7" />
                    
Directory.Packages.props
<PackageReference Include="WebSpark.Bootswatch" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add WebSpark.Bootswatch --version 1.0.7
                    
#r "nuget: WebSpark.Bootswatch, 1.0.7"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=WebSpark.Bootswatch&version=1.0.7
                    
Install WebSpark.Bootswatch as a Cake Addin
#tool nuget:?package=WebSpark.Bootswatch&version=1.0.7
                    
Install WebSpark.Bootswatch as a Cake Tool

WebSpark.Bootswatch

A .NET Razor Class Library for integrating Bootstrap 5 themes from Bootswatch into ASP.NET Core applications. This library simplifies the process of applying modern, responsive themes to your web applications, leveraging the power of Bootstrap 5.

NuGet License: MIT

Features

  • Seamless integration of Bootswatch themes with ASP.NET Core applications.
  • Built on Bootstrap 5, offering modern, responsive, and mobile-first design.
  • NEW! Integrated theme switcher component with light/dark mode support.
  • Easy runtime theme switching with cookie persistence.
  • Includes custom themes (e.g., Mom, Texecon) alongside standard Bootswatch themes.
  • Built-in caching mechanism for improved performance through StyleCache service.
  • Fully documented with IntelliSense support.

Benefits of Bootstrap 5

  • Responsive Design: Build mobile-first, responsive web applications effortlessly.
  • Modern Components: Access a wide range of pre-designed components.
  • Customizable: Easily customize themes to match your branding.
  • No jQuery Dependency: Bootstrap 5 removes the dependency on jQuery, making it lighter and faster.

Installation

Install the package via NuGet Package Manager:

Install-Package WebSpark.Bootswatch

Or via .NET CLI:

dotnet add package WebSpark.Bootswatch

Quick Start

1. Register Services

In the Program.cs file of your ASP.NET Core application, register the Bootswatch services:

var builder = WebApplication.CreateBuilder(args);

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

// Add Bootswatch theme switcher services (includes StyleCache)
builder.Services.AddBootswatchThemeSwitcher();

var app = builder.Build();

// Configure the HTTP request pipeline
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

// Use all Bootswatch features (includes StyleCache and static files)
app.UseBootswatchAll();

app.UseRouting();
app.MapRazorPages();

app.Run();

2. Update Your Layout

Modify your _Layout.cshtml file to use the theme switcher:

@using WebSpark.Bootswatch.Services
@using WebSpark.Bootswatch.Helpers
@inject StyleCache StyleCache
<!DOCTYPE html>
<html lang="en" data-bs-theme="@(BootswatchThemeHelper.GetCurrentColorMode(HttpContext))">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - WebSpark.Bootswatch.Demo</title>
    
    @{
        var themeName = BootswatchThemeHelper.GetCurrentThemeName(HttpContext);
        var themeUrl = BootswatchThemeHelper.GetThemeUrl(StyleCache, themeName);
    }
    
    <link id="bootswatch-theme-stylesheet" rel="stylesheet" href="@themeUrl" />
    <script src="/_content/WebSpark.Bootswatch/js/bootswatch-theme-switcher.js"></script>
</head>
<body>
    
    
    
    @Html.Raw(BootswatchThemeHelper.GetThemeSwitcherHtml(StyleCache, HttpContext))
    
    
    @RenderBody()
    
    
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    @await RenderSectionAsync("Scripts", required: false)
</body>
</html>

No JavaScript implementation is needed - the theme switcher functionality is now built into the library!

Advanced Usage

Using the Integrated StyleCache Service

The package includes a built-in StyleCache service for improved performance:

// In your Controller or Razor Page
using WebSpark.Bootswatch.Services;

public class HomeController : Controller
{
    private readonly StyleCache _styleCache;

    public HomeController(StyleCache styleCache)
    {
        _styleCache = styleCache;
    }

    public IActionResult Index()
    {
        // Get all available styles
        var styles = _styleCache.GetAllStyles();
        
        // Get a specific style by name
        var defaultStyle = _styleCache.GetStyle("default");
        
        return View();
    }
}

Theme Switcher Helpers

The library now includes the BootswatchThemeHelper class with useful methods:

// Get the current theme name
var themeName = BootswatchThemeHelper.GetCurrentThemeName(HttpContext);

// Get the current color mode (light/dark)
var colorMode = BootswatchThemeHelper.GetCurrentColorMode(HttpContext);

// Get the URL for a theme
var themeUrl = BootswatchThemeHelper.GetThemeUrl(StyleCache, themeName);

// Get HTML for the theme switcher component
var switcherHtml = BootswatchThemeHelper.GetThemeSwitcherHtml(StyleCache, HttpContext);

For more details on the theme switcher, see ThemeSwitcherGuide.md.

Demo Project

For a complete example of how to integrate WebSpark.Bootswatch, refer to the WebSpark.Bootswatch.Demo project included in this repository. It demonstrates:

  • Registering Bootswatch services.
  • Using the theme switcher component.
  • Applying themes dynamically in the layout.
  • Using the StyleCache service for efficient theme management.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.10.3 24 5/21/2025
1.10.2 29 5/20/2025
1.10.1 50 5/18/2025
1.10.0 204 5/15/2025
1.0.7 205 5/15/2025
1.0.6 136 5/5/2025
1.0.5 130 5/5/2025
1.0.4 128 5/4/2025
1.0.3 128 5/4/2025
1.0.2 129 5/4/2025

Version 1.0.7:
     - Added support for new Bootswatch themes
     - Improved documentation and examples
     - Fixed minor bugs and performance issues