Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NavigationError on NavigateTo

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.

like image 274
NPadrutt Avatar asked Sep 24 '19 09:09

NPadrutt


People also ask

How do I get the current URL in Blazor?

Inject NavigationManager in razor. Use Uri from NavigationManager to get the current URL.

How do I navigate to another page in Blazor?

You can redirect to a page in Blazor using the Navigation Manager's NavigateTo method. In the following code snippet, it will redirect to the home page when this page gets loaded. Similarly, you can call NavigateTo() method from NavigationManager class anywhere to redirect to another page.

How do you inject the NavigationManager in Blazor?

Access to browser navigation from Blazor is provided via the NavigationManager service. This can be injected into a Blazor component using @inject in a razor file, or the [Inject] attribute in a CS file. The NavigationManager service has two members that are of particular interest; NavigateTo and LocationChanged .


2 Answers

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.

like image 59
Pascal R. Avatar answered Sep 20 '22 16:09

Pascal R.


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>
like image 29
ataraxia Avatar answered Sep 22 '22 16:09

ataraxia