A flexible dynamic routing library for Blazor applications that enables runtime navigation without @page directives, providing type-safe navigation and efficient state management.
- π Dynamic Route Resolution - Navigate to components at runtime without @page directives
- π Type-Safe Navigation - Strongly-typed navigation with compile-time safety
- π¦ Efficient State Management - Binary serialization with optional compression
- π WebAssembly Compatible - Works with both Blazor Server and WebAssembly
- β‘ Lightweight URLs - FNV-1a hashing and compression for compact URLs
- π§ Extensible Architecture - Replace core services with custom implementations
Install via NuGet Package Manager:
dotnet add package Codezerg.DynamicRouter
Or via Package Manager Console:
Install-Package Codezerg.DynamicRouter
In your Program.cs
:
builder.Services.AddDynamicRouter(options =>
{
options.DynamicRoutePrefix = "dynamic";
options.Serialization.EnableCompression = true;
});
Create a catch-all route component to enable dynamic routing:
@* Pages/DynamicCatchAll.razor *@
@page "/dynamic/{**catch-all}"
@* This component's content is never displayed *@
In your Routes.razor
or App.razor
:
<DynamicRouter AppAssembly="typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="routeData" DefaultLayout="typeof(MainLayout)" />
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<p role="alert">Sorry, there's nothing at this address.</p>
</NotFound>
</DynamicRouter>
@inject IDynamicNavigationService Navigator
@code {
void NavigateToCounter()
{
// Navigate without parameters
Navigator.NavigateTo<Counter>();
// Navigate with parameters
Navigator.NavigateTo<Counter>(new { CurrentCount = 10 });
// Get URL for navigation
var url = Navigator.GetUrl<Counter>(new { CurrentCount = 5 });
}
}
The library supports all .NET primitive types via the TypeCode system:
- Integers:
byte
,sbyte
,short
,ushort
,int
,uint
,long
,ulong
- Floating-point:
float
,double
,decimal
- Text:
string
,char
- Boolean:
bool
- Date/Time:
DateTime
Complex objects and collections are not currently supported.
builder.Services.AddDynamicRouter(options =>
{
// URL prefix for dynamic routes (default: "dynamic")
options.DynamicRoutePrefix = "app";
// Enable state compression (default: true)
options.Serialization.EnableCompression = true;
// Compression level (default: Optimal)
options.Serialization.CompressionLevel = CompressionLevel.Fastest;
});
Implement your own navigation logic:
public class CustomNavigationService : IDynamicNavigationService
{
// Implementation
}
// Register
builder.Services.AddDynamicRouter()
.AddNavigationService<CustomNavigationService>();
Implement custom serialization:
public class JsonStateSerializer : IRouteStateSerializer
{
// Implementation
}
// Register
builder.Services.AddDynamicRouter()
.AddStateSerializer<JsonStateSerializer>();
// Navigate using Type
Navigator.NavigateTo(typeof(Counter), new { CurrentCount = 10 });
// Navigate using assembly and type name
Navigator.NavigateTo("MyApp", "MyApp.Pages.Counter", new { CurrentCount = 10 });
The repository includes two complete example applications:
- Blazor Server Example -
src/Codezerg.DynamicRouter.Example
- Blazor WebAssembly Example -
src/Codezerg.DynamicRouter.WasmExample
Run the examples:
# Blazor Server
cd src/Codezerg.DynamicRouter.Example
dotnet run
# Blazor WebAssembly
cd src/Codezerg.DynamicRouter.WasmExample
dotnet run
- Component Discovery - Scans assemblies for ComponentBase-derived types on initialization
- Type Identification - Uses FNV-1a 32-bit hash of type names for compact, WebAssembly-compatible identification
- State Encoding - Binary serialization of parameters with optional Deflate compression
- URL Generation - Base64url encoding for URL-safe state representation
- Route Resolution - Runtime matching and component instantiation with parameters
- .NET 9.0 or later
- Blazor Server or Blazor WebAssembly
- A catch-all route component for dynamic navigation
- Architecture - Design decisions and technical details
- Development Guide - Contributing and development setup
- API Reference - Complete API documentation
Contributions are welcome! Please read our Development Guide before submitting pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.
- π Report Issues
- π¬ Discussions
- π§ Contact: support@codezerg.com
Built with β€οΈ for the Blazor community.