Search code examples
asp.net-coreblazor

Why NavigationManager doesn't work in a button event?


@inject NavigationManager N

<button type="button" @onclick="Nav">test</button>  

@code
{    
    private void Nav()
    {      
        N.NavigateTo("/t");
    }
}

I have the razor file with @page "/t".

I clicked the test button, but nothing happened - why?


Solution

  • Is that the entire code of your blazor component? If you didn't add the render mode globally, then you need to add the code below for each component.

    • Add @rendermode InteractiveServer for Blazor server
    • Add @rendermode InteractiveWebAssembly for Blazor WASM
    • Add @rendermode InteractiveAuto for hybrid
    @rendermode InteractiveServer
    @inject NavigationManager N
    
    <button type="button"   @onclick="Nav">test</button>  
    
    @code
    {    
        private void Nav()
        {      
            N.NavigateTo("/t");
        }
    }