[英]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.