2

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.

5
  • Possible duplicate of: stackoverflow.com/questions/58076758/… ? Commented Jul 20, 2021 at 13:42
  • If the above answer doesn't fit, show some more code. There's no context in a single line of code! Commented Jul 20, 2021 at 14:03
  • 1
    Are you able to run it in Visual Studio's debug mode? Might provide some more info. Also, does /dashboard load when you go to the page directly? Commented Jul 20, 2021 at 15:25
  • Hi @XDboy, any update? Did my answer help you resolve your issue? Commented Jul 22, 2021 at 8:37
  • I already fixes it in another page with a button which has a href to my dashboard. I didnt try your code @Rena but I think to navigate with OnInitalized should work. Thanks for the answers :) Commented Jul 23, 2021 at 21:53

6 Answers 6

2

stranger. Set return type Task instead of void.

// this throw an Exception
protected async void OnLogin()
{
    navigationManager.NavigateTo("/");
}

// this this works in my case
protected async Task OnLogin()
{
    navigationManager.NavigateTo("/");
}
Sign up to request clarification or add additional context in comments.

Comments

1

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");  
        }
    }
}

Comments

0

I was facing the same problem and I solved it by making the function async and adding await Task.CompletedTask after like this:

private async Task YourFunction()
{
    if (yourIfCondition)
    {

    }
    else
    {
        NavManager.NavigateTo($"/dashboard");
        await Task.CompletedTask;
    }
}

I read that's due to how the framework is developed. It's supposed to throw the exception before actually navigating to the new element but it's not very convenient when developing.

Comments

0

I normally do not answer questions anymore, but this is one is old and seems that most answers are not correct.

In your OnInitialized, either variety, you need to set a boolean.

Reason event lifecycle is OnInitialized fires then OnAfterRender will fire.

So the clean way to do this is

private bool InitializationFailed  = false;

protected override void OnInitialized()
{     
     InitializationFailed = false;
     if (x == 5)
     {
          InitializationFailed = true;
     }
}

 protected override async Task OnAfterRenderAsync(bool firstRender)
 {
     if (InitializationFailed) 
     {
          NavManager.NavigateTo(NavManager.BaseUri);
     }

     await base.OnAfterRenderAsync(firstRender);
}

That is it and it does work

1 Comment

yes the only answer that worked for me
0

I know i'm a bit late but thought this may help.

I read this issue on the GitHub
https://github.com/dotnet/aspnetcore/issues/52777

"when a component is rendered using static server rendering, NavigationManager.NavigateTo() will always throw an exception. This is by design. The exception is meant to be caught by the framework so it can be handled as a redirect."

When you invoke NavigationManager.NavigateTo() the method is supposed to throw a NavigationException.

So pretty much this exception isn't supposed to bubble up as an unhandled exception. but Instead, it gets caught by Blazor framework (Inside Microsoft.AspNetCore.Components.Endpoints.dll), which then goes on to interprets it as a signal to perform a new redirect after the prerendered content is delivered.

So if you see this when using Blazor SSR in your debugging window or logs I wouldnt worry if your page is navigating correctly.

I hope this helps.

Comments

-1

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

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

2 Comments

This isn't required - NavManager.NavigateTo("/fetchdata"); works just fine.
ty for info, Shaun. I needed it, but I think it's because my Blazor project is a sub-app of my main site. But I doubt that has anything to do with the OP question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.