我目前正在使用Blazor Server开发一个登录和注册页面。在我的表单中提交字段后,有没有办法重定向到另一个.rarzor页面?我正在调用submitFields函数以通过"@onclick";此特定示例的事件处理程序。在submitFields函数中,我尝试过使用导航管理器,但它什么也没用。此外,我尝试重新初始化页面,并将检查设置为true,这样我就可以重定向,但我也遇到了这种方法的问题。有什么想法吗?下面是我的源代码。
@code {
private string email;
private string password;
private bool check = false;
private string mySetting = "";
// form input logic
private void submitFields()
{
User person = new User();
person.connect = mySetting;
person.email = email;
person.password = password;
//when decrypting use Encrypt.hashString(password)
person.logIn();
check = person.check;
// I want to redirect to another page here
/*
if(check == true)
{
yay youre logged in, lets go to a new page!
} else
{
dont go anywhere
}
*/
}
// on load -> call this func
private void initMethod()
{
mySetting = _config.GetValue<string>("MySetting"); //on load get hidden connection string from appsettings.json
}
// on load
protected override async Task OnInitializedAsync()
{
initMethod();
}
}
首先必须注入NavigationManager。然后您可以使用它导航到另一个页面。示例:
@inject NavigationManager NavigationManager
@code {
private void submitFields()
{
User person = new User();
person.connect = mySetting;
person.email = email;
person.password = password;
//when decrypting use Encrypt.hashString(password)
person.logIn();
check = person.check;
// I want to redirect to another page here
if(check == true)
{
NavigationManager.NavigateTo("/account/dashboard");
} else
{
dont go anywhere
}
}
}