BlazorRouting 1.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package BlazorRouting --version 1.0.3                
NuGet\Install-Package BlazorRouting -Version 1.0.3                
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="BlazorRouting" Version="1.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BlazorRouting --version 1.0.3                
#r "nuget: BlazorRouting, 1.0.3"                
#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.
// Install BlazorRouting as a Cake Addin
#addin nuget:?package=BlazorRouting&version=1.0.3

// Install BlazorRouting as a Cake Tool
#tool nuget:?package=BlazorRouting&version=1.0.3                

Blazor Routing

Have greater control over Blazor routing as well as using deeplinks with routable components.

I wanted to be able to deep link into my application but all the useful routing related classes in Microsoft.AspNetCore.Components.Routing were internal.

The objectives were to:

  1. Parse the browser location string to be able to activate components within the application instead of just 'pages'. This allowed routing the side-panels, etc.
  2. Use 'catch-all' routes to allow sub-routes to be handled by specific pages. Although the catch-all route was implemented in the current aspnetcore repository, it didn't appear to be present in the releases or any of the documentation.

The microsoft implementation for the RouteContext mutated the state of the context instance over multiple RouteEntry match invocations, but only the first match appears to ever be used. The implementation has be refactored to allow matching a RouteEntry and either using the handler specified in the entry or just taking some action. I had implemented a generic RouteEntry because some of the earlier iterations involved creating a entry with a delegate callback.

Usage

Implementing RoutableComponentBase

To Make a routable component:

  1. inherit from RoutableComponentBase
  2. specify the route template string

Optionally override OnActivate or OnActivateAsync to perform an action once the route is active and OnDeactivate or OnDeactivateAsync for deactivation.

@* inherit from RoutableComponentBase *@
@inherit RoutableComponentBase

...

@code {
  // Specify route template string
  protected override string Route => "/issues/add";

  protected override Task OnActivateAsync(Dictionary<string, object?> parameters)
  {
    ...
  }

  protected override void OnActivate(Dictionary<string, object?> parameters)
  {
    ...
  }

  protected override Task OnDeactivateAsync()
  {
    ...
  }

  protected override void OnDeactivate()
  {
    ...
  }
}

Providing the Location

Reference the library and replace the Router used in App.Razor. The same syntax is used. The RoutedComponentBase expects a cascading value called Location. The can be provided many different ways, but if the whole application has access to this cascading value. The Location is the current browser location.

<CascadingValue Value="Location" Name="Location">
    <Router AppAssembly="@typeof(Program).Assembly">
        ...
    </Router>
</CascadingValue>

The NavigationManager can be used to provide the Location

@inject NavigationManager NavigationManager

@code {
    private string? Location { get; set; }

    protected override void OnInitialized()
    {
        Location = NavigationManager.Uri; // Get initial location
        NavigationManager.LocationChanged += LocationChanged; // Get location changes
    }

    private void LocationChanged(
        object sender,
        Microsoft.AspNetCore.Components.Routing.LocationChangedEventArgs args)
    {
        Location = args.Location;
        StateHasChanged();
    }
}

This can be easily converted to a component.

@inject NavigationManager NavigationManager

<CascadingValue Value="Location" Name="Location">
    @ChildComponents
</CascadingValue>

@code {
    private string? Location { get; set; }

    [Parameter] public RenderFragment? ChildComponents { get; set; }

    protected override void OnInitialized()
    {
        Location = new Uri(NavigationManager.Uri).PathAndQuery;
        NavigationManager.LocationChanged += LocationChanged;
    }

    private void LocationChanged(
        object sender,
        Microsoft.AspNetCore.Components.Routing.LocationChangedEventArgs args)
    {
        Location = new Uri(args.Location).PathAndQuery;
        StateHasChanged();
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
2.0.2 440 1/13/2021
2.0.0 232 10/25/2020
1.0.4 495 9/26/2020
1.0.3 539 9/26/2020