NavigationException: "Exception_WasThrown" in Blazor

Viewed 1033

I try to navigate to another page with but I get every time the exeption: "Microsoft.AspNetCore.Components.NavigationException: Exception_WasThrown". Do you now what I need to do or what is wrong here?

on my dashboard page I have @page "/dashboard", so I dont think the string is wrong.

2 Answers

Did you add this code in @code{}? If not, it will get such exception but if you still F5 to continue the debug, it will then render the redirect page. This is a first chance exception that is thrown to cause a redirect to happen during pre-rendering. A workaround is to turn off first-chance exceptions you won't see this happen, you could check this github issue.

@page "/"
@inject NavigationManager NavManager
    
<p>Redirecting to Page</p>
@if (condition) {
    NavManager.NavigateTo($"/dashboard");   
}

Another workaround is that you could put these code to @code{}:

@page "/"
@inject NavigationManager NavManager        
<p>Redirecting to Page</p>    
@code {
    protected override void OnInitialized()
    {
        @if (condition) {
            NavManager.NavigateTo($"/dashboard");  
        }
    }
}

I'm not a navigation expert, but I think maybe you need to get the base:

LoginNavigator.NavigateTo(LoginNavigator.BaseUri + $"/dashboard", true);

Related