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?
By using Blazor route parameters, you can pass values from one page to another page. Use NavigationManager to pass a value and route the parameter to another page. Follow this example to achieve passing values from one page to another. Get the passed Page1 parameter value in Page2.
Inject NavigationManager in razor. Use Uri from NavigationManager to get the current URL.
The easiest way is to use Route parameters instead of QueryString: @page "/navigatetopage/{id:int}/{key}" @code { [Parameter] public int Id{get;set;} [Parameter] public string Key{get;set;} ... }
The Blazor Server hosting model offers several benefits: Download size is significantly smaller than a Blazor WebAssembly app, and the app loads much faster. The app takes full advantage of server capabilities, including the use of . NET Core APIs.
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");
}
}
After time and time of experimenting, I found out that in server side this is working:
using Microsoft.AspNetCore.Blazor.Services;
(...)
UriHelper.NavigateTo("/route");
Granted, looks almost the same, but does the work (at least in Blazor 0.8)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With