Skip to content

lantean-code/Blazor.HashRouting

Repository files navigation

Blazor.HashRouting

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.

Installation

dotnet add package Blazor.HashRouting

Usage

Register 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;
});

URL shape

  • Root route: /#/
  • Nested route: /#/foo
  • Base-path route: /proxy/app/#/foo

Relative links

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.

Supported environments

  • Blazor WebAssembly in a browser

Limitations

  • 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.

About

Hash fragment routing services for Blazor WebAssembly applications.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors