使用参数重定向blazor

Ber*_*ian 4 c# routing blazor

您好,如何Blazor使用参数重定向到另一个页面?

@page "/auth"
@using Microsoft.AspNetCore.Blazor.Services;
@inject AuthService auth
@inject IUriHelper urihelper;

<input type="text" bind="@Username" />
<button onclick="@AuthAsync">Authenticate</button>



@functions{

    public string Username { get; set; }
    public string url = "/home";

    public async Task AuthAsync()
    {
        var ticket=await this.auth.AuthenticateAsync(Username);
        urihelper.NavigateTo(url); //i see no overload that accepts parameters
    }
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我想导航到/home页面并为其提供一个字符串作为参数。

Isa*_*aac 5

做这个:

  • 像这样创建一个home.cshtml文件页面:注意,由于尚不支持可选参数,因此使用了两个@page指令。第一个允许导航到不带参数的组件。第二个@page指令采用{username}路由参数,并将该值分配给Username属性。

Pages / home.cshtml

@page "/home"
@page "/home/{username}"

<h1>@Username is authenticated!</h1>

@functions {
    // Define a property to contain the parameter passed from the auth page
    [Parameter]
    private string Username { get; set; };
}
Run Code Online (Sandbox Code Playgroud)
  • 在您的auth.cshtml中执行此操作
@page "/home"
@page "/home/{username}"

<h1>@Username is authenticated!</h1>

@functions {
    // Define a property to contain the parameter passed from the auth page
    [Parameter]
    private string Username { get; set; };
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助...