【问题标题】:Razor Pages RedirectToPage("/") causes error "No page named '/' matches the supplied values"Razor Pages RedirectToPage("/") 导致错误“没有名为 '/' 的页面与提供的值匹配”
【发布时间】:2020-10-03 02:31:29
【问题描述】:

在一个全新的开箱即用的 asp.net core RazorPages 项目中,为什么会出现以下异常“InvalidOperationException:没有名为 '/' 的页面与提供的值匹配”?

public IActionResult OnPost()
{
    return RedirectToPage("/");
}

我希望它重定向到 /Index。 RedirectToPage("")RedirectToPage("Index") 按预期工作,但在这种情况下,我被传递了一个“/”的返回 url。我知道我可以检查“/”并将其替换为“”,但这似乎应该可行。

【问题讨论】:

  • 其实我现在意识到 RedirectToPage("") 只是把我送回当前页面,所以我需要用“Index”替换“/”。

标签: razor-pages


【解决方案1】:

RedirectToPage() 方法指的是 Razor cshtml“页面”,而不是浏览器中的 html 页面。所以它期望参数引用你项目中的一个cshtml页面。例如

public IActionResult OnPost()
{
    // Redirect to Index.cshtml in folder above
    return RedirectToPage("../Index");
}

这就是为什么“/”在此方法中无效的原因。

如果你想重定向到一个网址,你可以使用Redirect() 例如

public IActionResult OnPost()
{
    return Redirect("~/");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    • 2021-04-18
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多