Skip to main content

Navigator

Navigator is a small router for Wisej.NET applications built from several Pages. You map paths to pages, and the Navigator shows the right page for the path after the # in the browser's URL. For example, https://app.example.com/#customers/42 shows the customer page with id = 42. This gives the application:

  • deep links that users can bookmark and share;
  • working Back and Forward buttons, since every navigation changes the URL hash;
  • route parameters (customers/{id}) and query parameters (?tab=orders);
  • an optional login page shown for every route until the user is authenticated.
Work in progress

This extension is marked work in progress in its source repository, and it isn't published on NuGet. Its API may change, and some members are placeholders. ExitPage, for example, can be set but isn't used yet. Test it thoroughly before you rely on it.

Installation​

Build Wisej.Web.Ext.Navigator from the source repository and reference the assembly from your Wisej.NET project. It targets .NET Framework 4.8 and .NET 8 or later.

Navigator is a static class with one instance per session. It has nothing to add to the toolbox. It listens to Application.HashChanged and to the application start and refresh events, and it navigates whenever the hash changes.

Getting started​

Map the routes when the session starts, usually in Program.Main, then navigate to the current hash:

using Wisej.Web.Ext.Navigator;

static class Program
{
static void Main()
{
// "/" is the home page.
Navigator.HomePage = new HomePage();

// A page created on first use and kept for the session.
Navigator.Map("customers", typeof(CustomersPage));

// A route with a parameter: #customers/42 sets Parameters["id"] = "42".
Navigator.Map("customers/{id}", () => new CustomerPage(), NavigatorPageMode.Dispose);

Navigator.Map("reports", typeof(ReportsPage));

// Show the page for the URL the user opened.
Navigator.Navigate(Application.Hash);
}
}

To move between pages, call Navigate from anywhere in the application, or put #path links in your HTML content:

Navigator.Navigate("customers/42?tab=orders");

Navigate hides the current page (and disposes it, if its route uses NavigatorPageMode.Dispose), shows the new one, fires the change events and updates the URL hash.

Routes​

Map has three overloads, one for each way the page is provided:

OverloadThe page is…
Map(path, pageType, mode)created with its parameterless constructor on first navigation.
Map(path, callback, mode)created by your Func<Page> on first navigation, for pages that need constructor arguments or dependency injection.
Map(path, page, mode)an instance you've already created.

The NavigatorPageMode decides what happens when the user navigates away:

  • Persist (the default) hides the page and keeps it, with its state, for the next visit;
  • Dispose disposes the page, and the next visit creates a new one. Only use it with the pageType and callback overloads, since a disposed page instance can't be shown again.

Matching. A leading / is ignored, so "customers" and "/customers" are the same route. When no route matches the path exactly, the Navigator uses the longest mapped prefix, case-insensitively. That's how customers/{id} handles customers/42. Remove(path) deletes a route, and Routes lists them all.

Parameters​

Navigator.Parameters is a NameValueCollection filled on every navigation from:

  • the query string of the hash: #reports?year=2026&format=pdf gives year and format;
  • the route placeholders: with the route customers/{id}/{tab}, #customers/42/orders gives id = "42" and tab = "orders".

Read them when the page is shown, or handle ParametersChanged:

public partial class CustomerPage : Page
{
public CustomerPage()
{
InitializeComponent();
Navigator.ParametersChanged += (s, e) => LoadCustomer();
}

private void CustomerPage_Appear(object sender, EventArgs e)
{
LoadCustomer();
}

private void LoadCustomer()
{
if (Navigator.CurrentPage != this)
return;

if (int.TryParse(Navigator.Parameters["id"], out var id))
this.customerBindingSource.DataSource = Customers.Find(id);
}
}
Validate parameters

Parameters come from the URL, which users can type and edit. Validate them, and check that the current user may see the record they point to, before loading data. A hidden route is not a security boundary.

Login page​

When LoginPage is set, every navigation shows the login page for as long as Authenticated is false. Setting Authenticated to true navigates to the path that was requested, so deep links survive the login. Setting it back to false navigates to /, which shows the login page again:

// Program.Main
Navigator.LoginPage = new LoginPage();

// LoginPage, after checking the credentials:
Navigator.Authenticated = true;

// Sign out:
Navigator.Authenticated = false;

Events​

EventWhen
CurrentPageChangedA navigation shows a different page. CurrentPage returns it.
ParametersChangedA navigation to a mapped route parses the parameters. In the current code this happens on practically every such navigation, even when the values are the same, so compare the values if that matters.

Both are static events of the session's Navigator. The sender is null. Detach handlers attached by pages that you dispose.

API reference​