0

I am creating a website/blog with a register/login/logout feature. I am almost done, but having a hard time with the Identities on the .Net8. The scaffolding feature changed and there were no YT videos to reference.

My NavMenu.razor shows the following code:

@inject NavigationManager Navigation

<div class="top-row container-fluid">
    <a class="navbar" href="">WebsiteProject</a>
    <input type="checkbox" title="Navigation menu" class="navbar-toggler" data-bs-toggle="collapse" data-bs-target="#navbar" />
</div>

<div class="nav-scrollable collapse navbar-collapse" id="navbar">
    <nav class="">
        <div class="nav-item px-3">
            <NavLink class="nav-link" href="" Match="NavLinkMatch.All">
                Home
            </NavLink>
        </div>
        <div class="nav-item px-3">
            <NavLink class="nav-link" href="Blog">
                Blog
            </NavLink>
        </div>
    </nav>
</div>


@code {
    private string? currentUrl;
    protected override void OnInitialized()
    {
        currentUrl = NavigationManager.ToBaseRelativePath(NavigationManager.Url);
        NavigationManager.LocationChanged += OnLocationChanged;
    }

    private void OnLocationChanged(object? sender, LocationChangedEventArgs e)
    {
        currentUrl = NavigationManager.ToBaseRelativePath(e.Location);
        StateHasChanged();
    }

    public void Dispose()
    {
        NavigationManager.LocationChanged -= OnLocationChanged;
    }

}

The following are the error codes that I can't seem to get rid of:

ErrorCodes from VS 2022

I have searched YT, Google, and even AI to see if it can help me find a way to fix my code, but no answers were found. Hence, I would like someone to help me troubleshoot my code.

1 Answer 1

0

As @inject NavigationManager Navigation, So the manager instance name is "Navigation". And it should be "Uri" instead of "Url".

@code {
    private string? currentUrl;
    protected override void OnInitialized()
    {
        currentUrl = Navigation.ToBaseRelativePath(Navigation.Uri);
        Navigation.LocationChanged += OnLocationChanged;
    }

    private void OnLocationChanged(object? sender, LocationChangedEventArgs e)
    {
        currentUrl = Navigation.ToBaseRelativePath(e.Location);
        StateHasChanged();
    }

    public void Dispose()
    {
        Navigation.LocationChanged -= OnLocationChanged;
    }
}
4
  • Hello, I tried the code provided, but I am getting the error CS0103 The name 'manager' does not exist in the current context.
    – Jax Harris
    Commented Jun 28 at 20:52
  • @JaxHarris Sorry ,it should be like "Navigation.Uri..." I have modify the answer you could try.
    – Qiang Fu
    Commented Jun 28 at 22:51
  • You are the man!!!!! Can you please explain what you changed?
    – Jax Harris
    Commented Jun 29 at 22:59
  • @JaxHarris I change all the "manager" to "Navigation" . It is because you inject "NavigationManager" as "Naviation". And use "Uri" instead of "Url"
    – Qiang Fu
    Commented Jul 1 at 6:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.