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.BootswatchOr via .NET CLI:
dotnet add package WebSpark.BootswatchIn 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();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>
<!-- Your layout content -->
<!-- Add the theme switcher where you want it to appear -->
@Html.Raw(BootswatchThemeHelper.GetThemeSwitcherHtml(StyleCache, HttpContext))
<!-- Content -->
@RenderBody()
<!-- Scripts -->
<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!
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 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.
For a complete example of how to integrate WebSpark.Bootswatch, refer to the WebSpark.Bootswatch.Demo project included in this repository. It demonstrates:
This project is licensed under the MIT License - see the LICENSE file for details.