如何重定向到主页?
How to redirect to Home page?
确认邮件页面包含:
public async Task<IActionResult> OnGetAsync(string userId, string code)
{
if (userId == null || code == null)
{
return RedirectToPage("/Index"); // <----------error
}
var user = await _userManager.FindByIdAsync(userId);
if (user == null)
{
return NotFound($"Unable to load user with ID '{userId}'.");
}
code = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(code));
var result = await _userManager.ConfirmEmailAsync(user, code);
StatusMessage = result.Succeeded ? "Thank you for confirming your email." : "Error confirming your email.";
return Page();
}
但是当我测试它的空参数时它会尝试重定向到页面
return RedirectToPage("/Index");
它产生异常:
An unhandled exception occurred while processing the request.
InvalidOperationException: No page named '/Index' matches the supplied
values
如何重定向到首页?
在 Blazor 中你应该使用 Microsoft.AspNetCore.Components.NavigationManager
.
@inject NavigationManager NavigationManager
if (userId == null || code == null)
{
NavigationManager.NavigateTo("/Index"); // <----------error
}
如果页面位于不同的文件夹中,它会以这种方式工作
return Redirect("~/");
RedirectToPage()
returns 一个 IActionResult
使 HTTP 重定向到您通过提供 Razor 页面 的路径指定的路由. Razor 页面是 Pages
文件夹中的 .cshtml
文件。
如果您使用的是 Blazor,那么您很可能正在使用它附带的客户端路由器。在这种情况下,只有一个 Razor 页面 _Host.cshtml
将充当 Blazor 应用程序的主要入口点。
所以您可以做的是重定向到/_Host
,尽管我不建议这样做。相反,只重定向到应用程序根目录,而不询问服务器端端点路由器路由是什么,可能是最好的主意:
return LocalRedirect("/");
确认邮件页面包含:
public async Task<IActionResult> OnGetAsync(string userId, string code)
{
if (userId == null || code == null)
{
return RedirectToPage("/Index"); // <----------error
}
var user = await _userManager.FindByIdAsync(userId);
if (user == null)
{
return NotFound($"Unable to load user with ID '{userId}'.");
}
code = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(code));
var result = await _userManager.ConfirmEmailAsync(user, code);
StatusMessage = result.Succeeded ? "Thank you for confirming your email." : "Error confirming your email.";
return Page();
}
但是当我测试它的空参数时它会尝试重定向到页面 return RedirectToPage("/Index");
它产生异常:
An unhandled exception occurred while processing the request. InvalidOperationException: No page named '/Index' matches the supplied values
如何重定向到首页?
在 Blazor 中你应该使用 Microsoft.AspNetCore.Components.NavigationManager
.
@inject NavigationManager NavigationManager
if (userId == null || code == null)
{
NavigationManager.NavigateTo("/Index"); // <----------error
}
如果页面位于不同的文件夹中,它会以这种方式工作
return Redirect("~/");
RedirectToPage()
returns 一个 IActionResult
使 HTTP 重定向到您通过提供 Razor 页面 的路径指定的路由. Razor 页面是 Pages
文件夹中的 .cshtml
文件。
如果您使用的是 Blazor,那么您很可能正在使用它附带的客户端路由器。在这种情况下,只有一个 Razor 页面 _Host.cshtml
将充当 Blazor 应用程序的主要入口点。
所以您可以做的是重定向到/_Host
,尽管我不建议这样做。相反,只重定向到应用程序根目录,而不询问服务器端端点路由器路由是什么,可能是最好的主意:
return LocalRedirect("/");