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:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container
builder.Services.AddRazorPages();
builder.Services.AddBootswatchStyles(); // Add Bootswatch services
var app = builder.Build();
// Configure the HTTP request pipeline
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseBootswatchStaticFiles(); // Enable Bootswatch static files
app.UseRouting();
app.MapRazorPages();
app.Run();
Create a partial view _ThemeSwitcher.cshtml to allow users to change themes:
@using WebSpark.Bootswatch.Model
@inject IStyleProvider StyleProvider
@{
var styles = await StyleProvider.GetAsync();
}
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
Change Theme
</button>
<ul class="dropdown-menu">
@foreach (var style in styles)
{
<li><a class="dropdown-item" href="?theme=@style.name">@style.name</a></li>
}
</ul>
</div>
Update your _Layout.cshtml file to use the current theme:
@using WebSpark.Bootswatch.Model
@inject IStyleProvider StyleProvider
@{
var theme = Context.Request.Query["theme"].ToString() ?? "default";
var currentStyle = await StyleProvider.GetAsync(theme);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - WebSpark.Bootswatch.Demo</title>
<!-- Use the theme from Bootswatch -->
@if (!string.IsNullOrEmpty(currentStyle.cssCdn))
{
<link rel="stylesheet" href="@currentStyle.cssCdn" />
}
else
{
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
}
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
</head>
<body>
<!-- Rest of your layout -->
<partial name="_ThemeSwitcher" />
<!-- Content -->
@RenderBody()
<!-- Scripts -->
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
@await RenderSectionAsync("Scripts", required: false)
</body>
</html>
For improved performance, you can implement a caching service:
public class StyleCache
{
private readonly IStyleProvider _styleProvider;
private readonly IMemoryCache _memoryCache;
private const string CacheKey = "BootswatchStyles";
public StyleCache(IStyleProvider styleProvider, IMemoryCache memoryCache)
{
_styleProvider = styleProvider;
_memoryCache = memoryCache;
}
public async Task<IEnumerable<StyleModel>> GetStylesAsync()
{
if (!_memoryCache.TryGetValue(CacheKey, out IEnumerable<StyleModel> styles))
{
styles = await _styleProvider.GetAsync();
_memoryCache.Set(CacheKey, styles, TimeSpan.FromHours(1));
}
return styles;
}
}
You can create your own theme provider by implementing the IStyleProvider interface:
public class CustomStyleProvider : IStyleProvider
{
public Task<IEnumerable<StyleModel>> GetAsync()
{
// Return your custom themes
}
public Task<StyleModel> GetAsync(string name)
{
// Return a specific theme by name
}
}
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.