如何重定向到asp.net核心剃刀页面(无路由)

6 c# asp.net-core razor-pages

在这里,我有一个剃须刀cs页面:

public IActionResult OnPost(){
  if (!ModelState.IsValid) {
    return Page(); 
  }

  return RedirectToPage('Pages/index.cshtml'); 

}
Run Code Online (Sandbox Code Playgroud)

和一个cshtml页面:

@page
@using RazorPages

@model IndexModel
<form method="POST">
  <input type="submit" id="Submit">
</form>
Run Code Online (Sandbox Code Playgroud)

我想重定向到表单提交的同一页面,但我不断收到400错误。是否可以在不使用路由的情况下进行重定向,而只需转到url中的cshtml文件?

S. *_*dal 7

查看 MS 页面 https://docs.microsoft.com/en-us/aspnet/core/mvc/razor-pages/?tabs=visual-studio

URL 路径与页面的关联由页面在文件系统中的位置确定。下表显示了 Razor 页面路径和匹配的 URL:

文件名路径匹配 URL
————————————————————————
/Pages/Index.cshtml / 或 /Index
/Pages/Contact.cshtml /Contact
/Pages/Store/Contact.cshtml /Store/Contact
/Pages/Store/Index.cshtml /Store 或 /Store/Index

页面的 URL 生成支持相对名称。下表显示了使用 Pages/Customers/Create.cshtml 中的不同 RedirectToPage 参数选择了哪个索引页面:

RedirectToPage(x) 页面
------------- ---------------------
RedirectToPage("/Index") 页数/索引
RedirectToPage("./Index"); 页面/客户/索引
RedirectToPage("../Index") 页数/索引
RedirectToPage("Index") 页面/客户/索引

  • 您好,请问您应该如何重定向到以下区域中的页面:/Areas/MyArea/Pages/Index.cshtml? (2认同)

小智 7

@Roman Pokrovskij这可能太旧了,但是如果您想重定向到某个区域,则应该:

return RedirectToPage ( "/Page", new { Area = "AreaName" } );
Run Code Online (Sandbox Code Playgroud)


Moh*_*med 5

在视图中试试这个;

@using (Html.BeginForm())
{
   <input type="submit" id="Submit">
}
Run Code Online (Sandbox Code Playgroud)