In Blazor, you can redirect to a different page with a parameter by using the NavigationManager service to navigate to a URL that includes the parameter.
Here's an example of how to redirect to a different page with a parameter in Blazor:
@inject NavigationManager NavigationManager <button @onclick="() => NavigationManager.NavigateTo($"/my-page/{parameterValue}")">Go to My Page</button> In this example, the NavigationManager service is injected into the component using the @inject directive. The NavigateTo method of the NavigationManager is called with a URL that includes the parameter value.
When the button is clicked, the user will be redirected to a URL like /my-page/parameterValue, where parameterValue is the value of the parameterValue variable.
In the target page, you can access the parameter value by defining a parameter on the page:
@page "/my-page/{parameterValue}" <h1>My Page</h1> <p>The parameter value is @parameterValue</p> @code { [Parameter] public string parameterValue { get; set; } } In this example, the @page directive specifies the URL pattern for the page, which includes the parameterValue parameter. The @Parameter directive is used to define the parameterValue property, which is set to the value of the parameter in the URL.
"Blazor redirect with parameter example"
// In the calling component NavigationManager.NavigateTo($"/TargetPage/{parameterValue}"); "Blazor navigation with parameter in URL"
// In the calling component NavigationManager.NavigateTo($"/TargetPage?parameter={parameterValue}"); "Blazor redirect with query string parameter"
// In the calling component NavigationManager.NavigateTo($"/TargetPage?parameter={parameterValue}"); "Blazor redirect with route parameter example"
// In the calling component NavigationManager.NavigateTo($"/TargetPage/{parameterValue}"); "Blazor passing parameters on page redirect"
NavigationManager to navigate to the target page with the specified parameter.// In the calling component NavigationManager.NavigateTo($"/TargetPage/{parameterValue}"); "Blazor redirect with complex object parameter"
// In the calling component NavigationManager.NavigateTo($"/TargetPage?parameter={JsonSerializer.Serialize(complexObject)}"); "Blazor redirect to page with parameter in code"
// In the calling component NavigationManager.NavigateTo($"/TargetPage/{parameterValue}"); "Blazor redirect with encrypted parameter"
// In the calling component var encryptedParameter = Encrypt(parameterValue); NavigationManager.NavigateTo($"/TargetPage/{encryptedParameter}"); "Blazor redirect with optional parameter in URL"
// In the calling component NavigationManager.NavigateTo($"/TargetPage/{(parameterValue != null ? parameterValue : string.Empty)}"); "Blazor redirect with parameter using route data"
// In the calling component NavigationManager.NavigateTo($"/TargetPage/{parameterValue}"); mozilla radix alpine-linux mouse-cursor stack space-analysis commando capitalize game-development uibarbuttonitem