The easy/stupid way.
I have two versions of NavMenu I want to display.
User is not logged in
User is logged in.
In NavMenu, I have a switch:
Partial code from NavMenu:
<div class="@NavMenuCssClass nav-scrollable">
<nav class="flex-column">
@if (UserName == "")
{//not logged in
<div class="nav-item px-3">
<NavLink class="nav-link" href="Register">
<span class="oi oi-list-rich" aria-hidden="true"></span> Register
</NavLink>
</div>
}
else
{//logged in
First visit UserName is a zero length string.
Now in the login page I put: (I know it is stupid, but it works)
This is AFTER the User has successfully entered UserName/Password:
if (success)
{
// This FORCES NavMenu.OnInitializedAsync() to run again.
// Passing a value in the QueryString allows NavMenu to reload with whatever
NavigationManager.NavigateTo("MyProject.Shared.NavMenu/LoggedIn", false, true);
}
So I call NavMenu DIRECTLY with NavigationManager, passing "LoggedIn" on the command line.
This forces NavMenu OnInitialized to run again and give a little info.
Back in NavMenu @code:
var uri = NavigationManager.ToAbsoluteUri(NavigationManager.Uri);
// If NavMenu is called directly, then I wanted it to reload.
// If the path was "LoggedIn" just go to Index
// The first time through (not logged in) UserName is blank!!
if (uri.AbsolutePath == "/TADEXT.Shared.NavMenu/LoggedIn")
{
NavigationManager.NavigateTo("/", false, true);
}
Sometime stupid things work great.