调用Razor页面操作



当在VS2019中使用个人用户帐户身份验证生成Blazor服务器端应用程序时,将创建以下代码:

<form method="post" action="Identity/Account/LogOut">
<button type="submit" class="nav-link btn btn-link">Log out</button>
</form>

这里我们有一个按钮,用于注销用户。我想从我自己的函数调用相同的操作,但我不知道如何发布到剃须刀页面。我该怎么做?

您可以使用NavigationManager 导航到页面

参见此示例代码:

@inject NavigationManager NavigationManager
<button class="btn btn-primary" @onclick="Logout">
Logout
</button>
@code {
private void Logout()
{
NavigationManager.NavigateTo("/Identity/Account/LogOut", true);
}
}

AreasIdentityPagesAccountLogOut.cshtml页面中,您需要将OnPost更改为OnGet或添加OnGet方法,如下所示:

public async Task<IActionResult> OnGet()
{
if (SignInManager.IsSignedIn(User))
{
await SignInManager.SignOutAsync();
}
return Redirect("~/");
}

感谢Henk Holterman所说的,您需要在NavigationManager.NavigateTo("...", true)中将forceRelead参数设置为true

相关内容

最新更新