Blazor Server App Nav Menu Item Page Caching

Howard Plonchak 1 Reputation point
2022-09-13T16:44:25.57+00:00

I've written a Blazor server app and the menu is not navigating properly when I use the same href. There's a parameter passed that should be displayed by the "generic.razor" destination. However, since the destination is the same and only the parameter is different, it's still ignoring the routing request. It's only after I navigate to a completely different page (generic2.razor) that it will properly take me to the generic2 and then clicking on generic will take my where I expect to go. Ideally, the navigation should include any params to determine whether to route to the page with the new parameter, or I should be able to force it to do so.

I've written a minimal sample project to show the issue. Hopefully there's a way to force the navigation to the same page (generic) but a different parameter.

<div class="nav-item px-3">
<NavLink class="nav-link" @onclick="() => ToggleNavMenu()" href="generic?texttoshow=Item1">
<span class="oi oi-plus" aria-hidden="true"></span> Item1
</NavLink>
</div>

    <div class="nav-item px-3">  
        <!-- Doesn't work if Item1 above is selected first -->  
        <NavLink class="nav-link" @onclick="() => ToggleNavMenu()" href="generic?texttoshow=Item2">  
            <span class="oi oi-plus" aria-hidden="true"></span> Item2  
        </NavLink>  
    </div>  

    <div class="nav-item px-3">  
        <!-- If I choose this in-between item1 and item2 above, navigating to generic after generic2 works as expected -->  
        <NavLink class="nav-link" @onclick="() => ToggleNavMenu()" href="generic2?texttoshow=Item3">  
            <span class="oi oi-plus" aria-hidden="true"></span> Item3  
        </NavLink>  
    </div>
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,403 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Howard Plonchak 1 Reputation point
    2022-09-16T00:45:26.687+00:00

    I've implemented a solution but it's really in a roundabout way. If I route the href in the navlink to an intervening page (redirect.razor) and have it literally redirect via NavigateTo to generic.razor, it works. But I did have to introduce a timer service in redirect.razor to wait a millisecond so as not to interfere with its own rendering. So the timer callback actually performs the NavigateTo. Apparently it's enough to clear the cached 'page' by inserting an intervening page. Shouldn't really be necessary and I hope you'll examine the code I provided and suggest to the developers that there must be a better, more flexible solution. If you want a zip of the entire sample project to repeat the behavior, do let me know how to get it to you.

    0 comments No comments