Get started with Blazor Bootstrap - .NET MAUI Blazor Hybrid app (.NET 10)
Install Blazor Bootstrap in a .NET 10 .NET MAUI Blazor Hybrid app: add the Blazor.Bootstrap NuGet package, add the Bootstrap 5.3 CSS and script references to wwwroot/index.html, register the services, and remove the template's own Bootstrap copy.
Install Nuget Package
Looking to quickly add Blazor Bootstrap to your project? Use NuGet package manager.
Install-Package Blazor.Bootstrap -Version 4.1.0
Add CSS references
After the <base href="/" /> tag in the head section in the wwwroot/index.html file, add the following references:
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-LN+7fdVzj6u52u30Kp6M/trliBMCMKTyK833zpbD+pXdCLuTusPj697FH4R/5mcr" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.13.1/font/bootstrap-icons.min.css">
<link href="_content/Blazor.Bootstrap/blazor.bootstrap.css" rel="stylesheet" />
Add script references
Insert the following references into the body section of the wwwroot/index.html file, immediately after the _framework/blazor.webview.js reference:
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/js/bootstrap.bundle.min.js" integrity="sha384-ndDqU0Gzau9qJ1lfW4pNLlhNTkCfHzAVBReH9diLvGRem5+R9g2FzA8ZGN954O5Q" crossorigin="anonymous"></script>
<!-- Add sortable.js reference if SortableList component is used in your application. -->
<script src="https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js"></script>
<script src="_content/Blazor.Bootstrap/blazor.bootstrap.js"></script>
Register services
Add Blazor Bootstrap service in the MauiProgram.cs
builder.Services.AddBlazorBootstrap();
Register tag helpers in _Imports.razor
@using BlazorBootstrap;
Remove default references
The default Blazor template includes demonstration code, icons, and Bootstrap. To remove these components, follow these steps:
-
Delete the bootstrap folder from the wwwroot directory:
- Delete the wwwroot/lib/bootstrap folder.
-
Remove the following line from wwwroot/index.html file:
<link rel="stylesheet" href="lib/bootstrap/dist/css/bootstrap.min.css" />
Starter template
GitHub Repo: Blazor Bootstrap - MAUI Blazor Hybrid App (.NET 10)
Sample Code
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover" />
<title>NET10.MauiBlazorHybridApp</title>
<base href="/" />
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-LN+7fdVzj6u52u30Kp6M/trliBMCMKTyK833zpbD+pXdCLuTusPj697FH4R/5mcr" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.13.1/font/bootstrap-icons.min.css">
<link href="_content/Blazor.Bootstrap/blazor.bootstrap.css" rel="stylesheet" />
<link rel="stylesheet" href="css/app.css" />
<link rel="stylesheet" href="NET10.MauiBlazorHybridApp.styles.css" />
<link rel="icon" type="image/png" href="favicon.png" />
</head>
<body>
<div class="status-bar-safe-area"></div>
<div id="app">Loading...</div>
<div id="blazor-error-ui">
An unhandled error has occurred.
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>
<script src="_framework/blazor.webview.js" autostart="false"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/js/bootstrap.bundle.min.js" integrity="sha384-ndDqU0Gzau9qJ1lfW4pNLlhNTkCfHzAVBReH9diLvGRem5+R9g2FzA8ZGN954O5Q" crossorigin="anonymous"></script>
<!-- Add sortable.js reference if SortableList component is used in your application. -->
<script src="https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js"></script>
<script src="_content/Blazor.Bootstrap/blazor.bootstrap.js"></script>
</body>
</html>
MauiProgram.cs
using Microsoft.Extensions.Logging;
namespace NET10.MauiBlazorHybridApp;
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});
builder.Services.AddMauiBlazorWebView();
builder.Services.AddBlazorBootstrap();
#if DEBUG
builder.Services.AddBlazorWebViewDeveloperTools();
builder.Logging.AddDebug();
#endif
return builder.Build();
}
}
_Imports.razor
@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
@using NET10.MauiBlazorHybridApp
@using NET10.MauiBlazorHybridApp.Components
@using BlazorBootstrap;
MainLayout.razor
@inherits LayoutComponentBase
<div class="bb-page">
<Sidebar Href="/"
IconName="IconName.BootstrapFill"
Title="Blazor Bootstrap"
DataProvider="SidebarDataProvider" />
<main>
<div class="bb-top-row px-4 d-flex justify-content-end">
<a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
</div>
<article class="content px-4">
<div class="py-2">@Body</div>
</article>
</main>
</div>
@code {
IEnumerable<NavItem>? navItems;
private async Task<SidebarDataProviderResult> SidebarDataProvider(SidebarDataProviderRequest request)
{
if (navItems is null)
navItems = GetNavItems();
return await Task.FromResult(request.ApplyTo(navItems));
}
private IEnumerable<NavItem> GetNavItems()
{
navItems = new List<NavItem>
{
new NavItem { Id = "1", Href = "/", IconName = IconName.HouseDoorFill, Text = "Home", Match=NavLinkMatch.All},
new NavItem { Id = "2", Href = "/counter", IconName = IconName.PlusSquareFill, Text = "Counter"},
new NavItem { Id = "3", Href = "/weather", IconName = IconName.Table, Text = "Fetch Data"},
};
return navItems;
}
}