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

user6781613 picture user6781613 · Aug 23, 2017 · Viewed 29.1k times · Source

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?

Answer

Breno Miyazaki picture Breno Miyazaki · Feb 6, 2019

@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" } );