Search code examples
c#asp.net-corerazor-pages

How to redirect to an ASP.NET Core Razor Page without using 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?


Solution

  • Try this in view;

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