Custom NavLink to Support Complex URL Matching in ASP.NET Blazor

To make the “Home” NavLink selected when navigating to http://localhost:3002/ or http://localhost:3002/Monitoring/, you can adjust the Match attribute of the NavLink to use a custom match condition. Blazor does not support complex URL matching directly out of the box, but you can achieve this by creating a custom CustomNavLink component.

CustomNavLink.razor

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@inject NavigationManager Navigation
@implements IDisposable
@if (IsActive)
{
<NavLink class="nav-link active" href="" Match="NavLinkMatch.All">
@ChildContent
</NavLink>
}
else
{
<NavLink class="nav-link" href="" Match="NavLinkMatch.All">
@ChildContent
</NavLink>
}
@code {
[Parameter] public RenderFragment ChildContent { get; set; } = default!;
private bool IsActive { get; set; }
protected override void OnInitialized()
{
Navigation.LocationChanged += OnLocationChanged;
}
private void OnLocationChanged(object? sender, LocationChangedEventArgs e)
{
// Handle the URL change here
IsActive = MatchUrl();
StateHasChanged(); // Update the UI
}
private bool MatchUrl()
{
var uri = NavigationManager.ToAbsoluteUri(NavigationManager.Uri).PathAndQuery;
if (uri.Equals("/"))
{
return true;
}
if (uri.StartsWith("/Monitoring", StringComparison.OrdinalIgnoreCase))
{
return true;
}
return false;
}
[Inject] private NavigationManager NavigationManager { get; set; } = default!;
public void Dispose()
{
Navigation.LocationChanged -= OnLocationChanged;
}
}
@inject NavigationManager Navigation @implements IDisposable @if (IsActive) { <NavLink class="nav-link active" href="" Match="NavLinkMatch.All"> @ChildContent </NavLink> } else { <NavLink class="nav-link" href="" Match="NavLinkMatch.All"> @ChildContent </NavLink> } @code { [Parameter] public RenderFragment ChildContent { get; set; } = default!; private bool IsActive { get; set; } protected override void OnInitialized() { Navigation.LocationChanged += OnLocationChanged; } private void OnLocationChanged(object? sender, LocationChangedEventArgs e) { // Handle the URL change here IsActive = MatchUrl(); StateHasChanged(); // Update the UI } private bool MatchUrl() { var uri = NavigationManager.ToAbsoluteUri(NavigationManager.Uri).PathAndQuery; if (uri.Equals("/")) { return true; } if (uri.StartsWith("/Monitoring", StringComparison.OrdinalIgnoreCase)) { return true; } return false; } [Inject] private NavigationManager NavigationManager { get; set; } = default!; public void Dispose() { Navigation.LocationChanged -= OnLocationChanged; } }
@inject NavigationManager Navigation
@implements IDisposable

@if (IsActive)
{
    <NavLink class="nav-link active" href="" Match="NavLinkMatch.All">
        @ChildContent
    </NavLink>
}
else
{
    <NavLink class="nav-link" href="" Match="NavLinkMatch.All">
        @ChildContent
    </NavLink>
}

@code {
    [Parameter] public RenderFragment ChildContent { get; set; } = default!;

    private bool IsActive { get; set; }

    protected override void OnInitialized()
    {
        Navigation.LocationChanged += OnLocationChanged;
    }
    
    private void OnLocationChanged(object? sender, LocationChangedEventArgs e)
    {
        // Handle the URL change here
        IsActive = MatchUrl();
        StateHasChanged(); // Update the UI
    }
    
    private bool MatchUrl()
    {
        var uri = NavigationManager.ToAbsoluteUri(NavigationManager.Uri).PathAndQuery;

        if (uri.Equals("/"))
        {
            return true;
        }

        if (uri.StartsWith("/Monitoring", StringComparison.OrdinalIgnoreCase))
        {
            return true;
        }
        
        return false;
    }

    [Inject] private NavigationManager NavigationManager { get; set; } = default!;
    public void Dispose()
    {
        Navigation.LocationChanged -= OnLocationChanged;
    }

}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<CustomNavLink>
<span class="bi bi-house-door-fill-nav-menu" aria-hidden="true"></span> Home
</CustomNavLink>
<CustomNavLink> <span class="bi bi-house-door-fill-nav-menu" aria-hidden="true"></span> Home </CustomNavLink>
<CustomNavLink>
    <span class="bi bi-house-door-fill-nav-menu" aria-hidden="true"></span> Home
</CustomNavLink>