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

5

14

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?

Mantoman answered 23/8, 2017 at 4:3 Comment(4)
return View(viewname);Thug
this is a razor pageMantoman
of type PageModelMantoman
Remove "Pages" from your redirect path.Compulsion
C
5

Try this in view;

@using (Html.BeginForm())
{
   <input type="submit" id="Submit">
}
Cannikin answered 23/8, 2017 at 4:9 Comment(2)
This works when RedirectToPage('Pages/index.cshtml'); is changed to RedirectToPage('Index');Mantoman
You could also just use RedirectToPage();Interim
V
12

@Roman Pokrovskij This might be way too much old but if you want to redirect to an Area you should:

return RedirectToPage ( "/Page", new { Area = "AreaName" } );
Vickery answered 6/2, 2019 at 18:53 Comment(0)
M
11

check out the MS page https://learn.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
Motorbus answered 8/9, 2017 at 7:48 Comment(2)
Hi, could I ask you how we should redirect to pages in the areas: /Areas/MyArea/Pages/Index.cshtml?Hula
The big GOTCHA for me was thinking I needed "Pages" or "/Pages" in the path. Once I removed it everything worked. REMOVE "Pages" FROM YOUR REDIRECT PATH.Compulsion
C
5

Try this in view;

@using (Html.BeginForm())
{
   <input type="submit" id="Submit">
}
Cannikin answered 23/8, 2017 at 4:9 Comment(2)
This works when RedirectToPage('Pages/index.cshtml'); is changed to RedirectToPage('Index');Mantoman
You could also just use RedirectToPage();Interim
H
3

If you want to redirect to a different page after some action:

return Redirect("~/Page/YourAction");
Haphazard answered 14/12, 2020 at 7:32 Comment(1)
Thank so i have RedirectToPage but it didn;t worked for me.Houston
G
1
return RedirectToPage("./Index");
Gynoecium answered 15/8, 2022 at 4:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.