OakLab.Blazor.Navigation 0.2.1

Prefix Reserved
dotnet add package OakLab.Blazor.Navigation --version 0.2.1
                    
NuGet\Install-Package OakLab.Blazor.Navigation -Version 0.2.1
                    
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="OakLab.Blazor.Navigation" Version="0.2.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OakLab.Blazor.Navigation" Version="0.2.1" />
                    
Directory.Packages.props
<PackageReference Include="OakLab.Blazor.Navigation" />
                    
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 OakLab.Blazor.Navigation --version 0.2.1
                    
#r "nuget: OakLab.Blazor.Navigation, 0.2.1"
                    
#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 OakLab.Blazor.Navigation@0.2.1
                    
#: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=OakLab.Blazor.Navigation&version=0.2.1
                    
Install as a Cake Addin
#tool nuget:?package=OakLab.Blazor.Navigation&version=0.2.1
                    
Install as a Cake Tool

Blazor Navigation

Build Nuget

Get rid of magic url strings and navigate to page classes.

navigationManager.NavigateTo<HomePage>();

No overrides to routing mechanism, just stateless extensions to navigation manager.

Usage

Route parameters

Supposedly you have route parameters on your page - /blogs/{BlogName}/posts/{PostId}/comments.

Route is parsed and argument templates are found which requires exactly to arguments to be passed to the function.

navigationManager.NavigateTo<CommentsPage>(blogName, postId);

Route arguments are matched by their order in route template.

Query parameters

There are three options to pass query parameters.

With plain string

navigationManager.NavigateWithQueryParametersTo<DashboardPage>("?From=2022-01-01&ShowInactive=true");

With enumeration of key value pairs

navigationManager.NavigateWithQueryParametersTo<DashboarPage>(new Dictionary<string, object>()
{
    ["From"] = "2022-01-01",
    ["ShowInactive"] = true
});

Type must derive from IEnumerable<KeyValuePair<string, object>>.

With plain old CLR object

navigationManager.NavigateWithQueryParametersTo<DashboardPage>(new
{
    From = new DateOnly(2022, 1, 1), // all parameters are formatted with invariant culture
    ShowInactive = true
});

With route parameters too

Route parameters goes after query parameters.

navigationManager.NavigateWithQueryParametersTo<DashboardPage>(queryParameters, routeParameter1, routeParameter2);

Route objects

This package introduces abstract base class Route<>. Every route class directs to single page which is specified by generic argument. One can navigate like:

navigationManager.NavigateTo(dashboardRoute);

where dashboardRoute is instance of Route<DashboardPage> inheritor.

Extension method takes all public properties and invokes two virtual methods for corresponding parameters - GetRouteParameters() and GetQueryParameters(). When method is not overriden then it reads arguments from public properties of the class, route parameters are matched by placeholder in route template and remaining are treated as query parameters.

For example assume route template /blogs/{BlogName}/posts/{PostId}/comments and route object

class CommentsRoute : Route<CommentsPage>
{
    public string BlogName { get; set; }
    public Guid PostId { get; set; }
    public PageSize PageSize { get; set; }
    public PageNumber PageNumber { get; set; }
}

In this scenario BlogName and PostId will be bound to route parameters where PageSize and PageNumber to query parameters.

Route objects can also work the other way around. GetCurrentRoute<TRoute>() extension method on NavigationManager allows to get route with properties bound to current location. Binding can be customized by overriding SetParameters(Uri) method on route object. Use GetRouteParametersFromUri(Uri) and GetQueryParametersFromUri(Uri) to extract parameters from uri in text format.

In order to convert objects to text and otherwise type converter for this type is used. Conversion is culture invariant.

To prevent property from binding annotate it with [RouteIgnore] attribute.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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.  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
0.2.1 584 9/10/2022
0.2.0 527 9/1/2022
0.1.0 538 7/21/2022