[英]How to redirect to a asp.net core razor page (no routes)
这里我有一个 razor cs 页面:
public IActionResult OnPost(){
if (!ModelState.IsValid) {
return Page();
}
return RedirectToPage('Pages/index.cshtml');
}
还有一个cshtml页面:
@page
@using RazorPages
@model IndexModel
<form method="POST">
<input type="submit" id="Submit">
</form>
我想在提交表单时重定向到同一页面,但我不断收到 400 错误。 是否可以在不使用路由的情况下进行重定向,只使用 go 到 url 中的 cshtml 文件?
@Roman Pokrovskij 这可能太旧了,但如果你想重定向到一个区域,你应该:
return RedirectToPage ( "/Page", new { Area = "AreaName" } );
查看 MS 页面https://docs.microsoft.com/en-us/aspnet/core/mvc/razor-pages/?tabs=visual-studio
URL 路径与页面的关联由页面在文件系统中的位置确定。 下表显示了 Razor 页面路径和匹配的 URL:
File name path matching URL --------------------------- ---------------------- /Pages/Index.cshtml / or /Index /Pages/Contact.cshtml /Contact /Pages/Store/Contact.cshtml /Store/Contact /Pages/Store/Index.cshtml /Store or /Store/Index
页面的 URL 生成支持相对名称。 下表显示了使用 Pages/Customers/Create.cshtml 中的不同 RedirectToPage 参数选择了哪个索引页面:
RedirectToPage(x) Page ------------------------ --------------------- RedirectToPage("/Index") Pages/Index RedirectToPage("./Index"); Pages/Customers/Index RedirectToPage("../Index") Pages/Index RedirectToPage("Index") Pages/Customers/Index
在视图中试试这个;
@using (Html.BeginForm())
{
<input type="submit" id="Submit">
}
如果您想在某些操作后重定向到不同的页面:
return Redirect("~/Page/YourAction");
return RedirectToPage("./Index");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.