简体   繁体   中英

How to redirect to a asp.net core razor page (no routes)

Here I have a razor cs page:

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

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

}

And a cshtml page:

@page
@using RazorPages

@model IndexModel
<form method="POST">
  <input type="submit" id="Submit">
</form>

I want to redirect to the same page on form submit but I keep getting 400 error. Is it possible to do the redirect without using routes and just go to cshtml file in the url?

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

return RedirectToPage ( "/Page", new { Area = "AreaName" } );

check out the MS page https://docs.microsoft.com/en-us/aspnet/core/mvc/razor-pages/?tabs=visual-studio

The associations of URL paths to pages are determined by the page's location in the file system. The following table shows a Razor Page path and the matching 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 generation for pages supports relative names. The following table shows which Index page is selected with different RedirectToPage parameters from Pages/Customers/Create.cshtml:

RedirectToPage(x)           Page
------------------------    ---------------------
RedirectToPage("/Index")    Pages/Index
RedirectToPage("./Index");  Pages/Customers/Index
RedirectToPage("../Index")  Pages/Index
RedirectToPage("Index")     Pages/Customers/Index

Try this in view;

@using (Html.BeginForm())
{
   <input type="submit" id="Submit">
}

如果您想在某些操作后重定向到不同的页面:

return Redirect("~/Page/YourAction");

return RedirectToPage("./Index");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM