WebSpark.Bootswatch provides Bootswatch themes for ASP.NET Core applications. It includes custom themes and styles that can be easily integrated with ASP.NET Core MVC or Razor Pages applications. Supports .NET 8.0, 9.0, and 10.0. ⚠️ IMPORTANT: This package requires WebSpark.HttpClientUtility to be installed and registered separately. SETUP: 1. Install: dotnet add package WebSpark.HttpClientUtility 2. Register: builder.Services.AddHttpClientUtility(); (BEFORE AddBootswatchThemeSwitcher) 3. Configure appsettings.json with HttpRequestResultPollyOptions section See package README for complete setup guide.
$ dotnet add package WebSpark.BootswatchA .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.
Install the package via NuGet Package Manager:
Install-Package WebSpark.Bootswatch
Or via .NET CLI:
dotnet add package WebSpark.Bootswatch
In the Program.cs file of your ASP.NET Core application, register the Bootswatch services. If you use the Tag Helper, also add AddHttpContextAccessor():
var builder = WebApplication.CreateBuilder(args);
// Add services to the container
builder.Services.AddRazorPages();
// (Optional) If you use the Tag Helper, add HttpContextAccessor
builder.Services.AddHttpContextAccessor();
// 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();
// Use all Bootswatch features (includes StyleCache and static files)
app.UseBootswatchAll();
// (Optional) Add custom static file logging middleware if desired
// app.Use(...)
app.UseStaticFiles();
app.UseRouting();
app.MapRazorPages();
app.Run();
In your _Layout.cshtml, place the theme switcher Tag Helper outside the <ul class="navbar-nav flex-grow-1"> and after the navigation links, as shown in the demo:
<!-- ...existing nav markup... -->
<ul class="navbar-nav flex-grow-1">
<!-- nav items -->
</ul>
<bootswatch-theme-switcher />
<!-- ...rest of layout... -->
This ensures the theme switcher appears in the correct place in the navigation bar, matching the demo project.
Register the Tag Helper in your _ViewImports.cshtml:
@addTagHelper *, WebSpark.Bootswatch
If you use the manual method, do not place both the Tag Helper and the manual HTML helper in the same layout to avoid duplicate selectors.
Manual alternative:
@Html.Raw(BootswatchThemeHelper.GetThemeSwitcherHtml(StyleCache, Context))
The rest of your layout setup remains the same. See the included sample layout file for a full example.
No JavaScript implementation is needed—the theme switcher functionality is built into the library!
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();
}
}
The library includes the BootswatchThemeHelper class with useful methods. In Razor Pages, use Context:
// Get the current theme name
var themeName = BootswatchThemeHelper.GetCurrentThemeName(Context);
// Get the current color mode (light/dark)
var colorMode = BootswatchThemeHelper.GetCurrentColorMode(Context);
// Get the URL for a theme
var themeUrl = BootswatchThemeHelper.GetThemeUrl(StyleCache, themeName);
// Get HTML for the theme switcher component
var switcherHtml = BootswatchThemeHelper.GetThemeSwitcherHtml(StyleCache, Context);
For more details on the theme switcher, see ThemeSwitcherGuide.md.
Follow these steps to add Bootswatch theme support and the integrated theme switcher to your existing ASP.NET Core Razor Pages or MVC project:
Install the package via NuGet Package Manager or .NET CLI:
Install-Package WebSpark.Bootswatch
or
dotnet add package WebSpark.Bootswatch
In your Program.cs, register the Bootswatch services after adding Razor Pages:
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();
// ...existing code for error handling, HSTS, etc...
app.UseHttpsRedirection();
// Use all Bootswatch features (includes StyleCache and static files)
app.UseBootswatchAll();
app.UseRouting();
app.MapRazorPages();
app.Run();
In your main layout file (e.g., _Layout.cshtml), add the following:
StyleCache service.BootswatchThemeHelper methods with Context (for Razor Pages) or HttpContext (for MVC) to set the theme and render the switcher.Example for Razor Pages:
@using WebSpark.Bootswatch.Services
@using WebSpark.Bootswatch.Helpers
@inject StyleCache StyleCache
<!DOCTYPE html>
<html lang="en" data-bs-theme="@(BootswatchThemeHelper.GetCurrentColorMode(Context))">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"]</title>
@{
var themeName = BootswatchThemeHelper.GetCurrentThemeName(Context);
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, Context))
@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>
You can inject and use the StyleCache service to access available styles or theme information in your controllers or pages.
public class HomeController : Controller
{
private readonly StyleCache _styleCache;
public HomeController(StyleCache styleCache)
{
_styleCache = styleCache;
}
public IActionResult Index()
{
var styles = _styleCache.GetAllStyles();
var defaultStyle = _styleCache.GetStyle("default");
return View();
}
}
Add these lines to your Program.cs:
builder.Services.AddBootswatchThemeSwitcher();
app.UseBootswatchAll();
You can use the built-in Tag Helper to render the theme switcher in your layout:
<bootswatch-theme-switcher />
Register the Tag Helper in your _ViewImports.cshtml:
@addTagHelper *, WebSpark.Bootswatch
A sample layout file is included in the NuGet package at:
contentFiles/any/any/BootswatchLayoutExample.cshtml
Copy or reference this file to quickly set up your layout.
For a complete example of how to integrate WebSpark.Bootswatch, refer to the WebSpark.Bootswatch.Demo project included in this repository. It demonstrates:
<bootswatch-theme-switcher />) for easy integration in layouts and navigation bars.AddHttpContextAccessor() when using the Tag Helper.This project is licensed under the MIT License - see the LICENSE file for details.