How to redirect to a different route in Blazor Server-side

mikeyy picture mikeyy · Jan 4, 2019 · Viewed 17.5k times · Source

In Blazor Client a redirection can be achieved using

using Microsoft.AspNetCore.Blazor.Browser.Services;
(...)
BrowserUriHelper.Instance.NavigateTo("/route")

This does however not work in a Blazor Server project, as it generates the following error:

Unable to cast object of type 'Microsoft.AspNetCore.Blazor.Server.Circuits.RemoteJSRuntime' to type 'Microsoft.JSInterop.IJSInProcessRuntime'.

How does a redirect in Blazor-Server look like?

Answer

Mehmet Taha Meral picture Mehmet Taha Meral · Oct 16, 2019

If you can trigger on the razor page, you can use the following:

@page "/YourPageName"
@inject NavigationManager NavigationManager

<h1>xxx</h1>
.
.
.


@code {

    void MethodToTriggerUrl()
    {
        NavigationManager.NavigateTo("PageToRedirect");
    }
}