NavigationError on NavigateTo
Solution 1
There is an open issue on github for this problem. See also the preceeding issue where a possible workaround is mentioned: putting the NavigateTo
method into OnAfterRender
instead of OnInitialized
.
Solution 2
I needed to put this into OnInitialized
rather than OnAfterRender
, so had to go with the render-mode="Server"
method, though apparently this isn't recommended.
It also states in the GitHub issue that this only happens in debug, not release, so a middle ground option is to change _Host.cshtml
to contain:
<environment include="Staging,Production">
<component render-mode="ServerPrerendered" type="typeof(App)" />
</environment>
<environment include="Development">
<component render-mode="Server" type="typeof(App)" />
</environment>
Solution 3
You may also change your method to the following including the "async" keyword to its signature, there is going to be a complaint about not using await, but in exchange you are not going to need a return value. Since it doesn't have an 'await' the effect is kinda the same as the synchronous version, but without the exception being thrown.
protected override async Task OnInitializedAsync()
{
NavigationManager.NavigateTo("Login");
}
Here is an example where i am using a RedirectToLogin.razor component in my routing
@inject NavigationManager NavigationManager
@code{
protected override async Task OnInitializedAsync()
{
var returnUrl = "~/" + NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
NavigationManager.NavigateTo($"Identity/Account/Login?returnUrl={returnUrl}", forceLoad:false);
}
}
And in my App.razor
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
<RedirectToLogin />
</NotAuthorized>
</AuthorizeRouteView>
</Found>
Solution 4
Old post, but - if you are running a Blazor Server app, this behavior only happens if render-mode is "ServerPrerendered". Disabling Pre-rendering by changing the mode to "Server" makes the exception not be thrown in the first place:
<app>
<component type="typeof(App)" render-mode="Server" />
</app>
I've searched current Blazor Documentation and change notes, and haven't found any mention of this, so just in case it helps somebody else...
Solution 5
I ran into the same problem and filed issue #28355. The official answer is, that it is save to ignore the exception when NaviagteTo
is placed in OnInitialized
. Here is the answer from javiercn:
Yes, it is completely safe to ignore it. The debugger stops because it is configured to do so, but in this case the exception is always handled. You can turn-off breaking on this exception if it's caught on the debugger settings.
Issue #13582 deals with how to prevent the debugger from stopping at this exception.
Comments
-
NPadrutt almost 2 years
I'm trying out Blazor ServerSide and created a Component to Redirect To the Login Page when a user is not logged in.
@inject Microsoft.AspNetCore.Components.NavigationManager NavigationManager; @code { /// <inheritdoc /> protected override Task OnInitializedAsync() { NavigationManager.NavigateTo("Login"); return Task.CompletedTask; }
}
But always when "NavigateTo" is called the following exception is thrown:
"Microsoft.AspNetCore.Components.NavigationException: Exception of type 'Microsoft.AspNetCore.Components.NavigationException' was thrown. at Microsoft.AspNetCore.Components.Server.Circuits.RemoteNavigationManager.NavigateToCore(String uri, Boolean forceLoad) at Microsoft.AspNetCore.Components.NavigationManager.NavigateTo(String uri, Boolean forceLoad) at ApplySupportTool.Blazor.Pages.RedirectToLogin.OnInitializedAsync() in C:\\Users\\padruttn\\Documents\\git\\ApplySupportTool\\src\\ApplySupportTool.Blazor\\Pages\\RedirectToLogin.razor:line 8"
Interesstingly the navigation is made despite the exception. I also tried to call it with the path "/login" but the same behaviour here.