使用参数重定向blazor [英] Redirecting in blazor with parameter
本文介绍了使用参数重定向blazor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,如何使用参数重定向到 Blazor
中的另一页?
Hello how can you redirect to another page in Blazor
with a parameter?
@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
}
}
的重载情况下,我想导航到 / home
页面,并给它一个字符串作为参数。
In this case i want to navigate to the /home
page giving it a string as parameter.
推荐答案
执行以下操作:
- 创建如下所示的home.cshtml文件页面:
注意,两个@page指令之所以使用,是因为尚不支持可选参数。
第一个允许不带参数的组件导航。第二个@page指令
使用{username}路由参数,并将值分配给Username属性。
@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; };
}
- 在auth.cshtml中执行此操作
@functions{
public string Username { get; set; }
public string url = "/home";
public async Task AuthAsync()
{
var ticket=await this.auth.AuthenticateAsync(Username);
// Attach the parameter to the url
urihelper.NavigateTo(url + "/" + Username);
}
}
希望这会有所帮助...
Hope this helps...
这篇关于使用参数重定向blazor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文