Blazor.HashRouting provides hash-fragment routing for browser-hosted Blazor WebAssembly applications.
It is designed for environments where the host can reliably serve only the application root document and cannot deep-link arbitrary route paths on refresh.
dotnet add package Blazor.HashRoutingRegister hash routing during startup:
using Blazor.HashRouting;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.Services.AddHashRouting();
var host = builder.Build();
await host.InitializeHashRoutingAsync();
await host.RunAsync();Call InitializeHashRoutingAsync() after Build() and before RunAsync().
Optional configuration:
builder.Services.AddHashRouting(options =>
{
options.HashPrefix = "/";
options.CanonicalizeToHash = true;
options.InterceptInternalLinks = true;
});- Root route:
/#/ - Nested route:
/#/foo - Base-path route:
/proxy/app/#/foo
Relative anchor href values such as ./bar are resolved relative to the application base URI, not the current hash route.
For example, from /#/foo, an anchor with href="./bar" canonicalizes to /#/bar, not /#/foo/bar.
- Blazor WebAssembly in a browser
- Not intended for Blazor Server.
- Not intended for Blazor Hybrid.
- Assumes the host can serve the application root document and static assets, but may not support deep-link route paths.