Codezerg.DynamicRouter 1.0.0

dotnet add package Codezerg.DynamicRouter --version 1.0.0
                    
NuGet\Install-Package Codezerg.DynamicRouter -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Codezerg.DynamicRouter" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Codezerg.DynamicRouter" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Codezerg.DynamicRouter" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Codezerg.DynamicRouter --version 1.0.0
                    
#r "nuget: Codezerg.DynamicRouter, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Codezerg.DynamicRouter@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Codezerg.DynamicRouter&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Codezerg.DynamicRouter&version=1.0.0
                    
Install as a Cake Tool

Codezerg.DynamicRouter

A flexible dynamic routing library for Blazor applications that enables runtime navigation without @page directives, providing type-safe navigation and efficient state management.

NuGet Version License: MIT .NET 9.0

Features

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

Installation

Install via NuGet Package Manager:

dotnet add package Codezerg.DynamicRouter

Or via Package Manager Console:

Install-Package Codezerg.DynamicRouter

Quick Start

1. Configure Services

In your Program.cs:

builder.Services.AddDynamicRouter(options =>
{
    options.DynamicRoutePrefix = "dynamic";
    options.Serialization.EnableCompression = true;
});

2. Create Catch-All Route (Required)

Create a catch-all route component to enable dynamic routing:

@* Pages/DynamicCatchAll.razor *@
@page "/dynamic/{**catch-all}"

@* This component's content is never displayed *@

3. Replace Router with DynamicRouter

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>

4. Navigate to Components

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

Supported Parameter Types

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.

Configuration Options

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

Advanced Usage

Custom Navigation Service

Implement your own navigation logic:

public class CustomNavigationService : IDynamicNavigationService
{
    // Implementation
}

// Register
builder.Services.AddDynamicRouter()
    .AddNavigationService<CustomNavigationService>();

Custom State Serializer

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

Examples

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

How It Works

  1. Component Discovery - Scans assemblies for ComponentBase-derived types on initialization
  2. Type Identification - Uses FNV-1a 32-bit hash of type names for compact, WebAssembly-compatible identification
  3. State Encoding - Binary serialization of parameters with optional Deflate compression
  4. URL Generation - Base64url encoding for URL-safe state representation
  5. Route Resolution - Runtime matching and component instantiation with parameters

Requirements

  • .NET 9.0 or later
  • Blazor Server or Blazor WebAssembly
  • A catch-all route component for dynamic navigation

Documentation

Contributing

Contributions are welcome! Please read our Development Guide before submitting pull requests.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

Acknowledgments

Built with ❤️ for the Blazor community.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 100 8/22/2025