title: ASP.NET Core redirecting to the same page after submitting a form with HttpPost method
body: <p>I am developing a web application using ASP.NET Core 2.0. 
  First of all i have a main view called NewOrder.cshtml:</p>
  
  <pre><code>@using Microsoft.AspNetCore.Mvc.Rendering
  @using MokaKukaMap.Orders.ViewModels.Models
  @model MokaKukaMap.Orders.ViewModels.Models.NewOrderViewModel
  
  @{
      Layout = "~/Views/Shared/_Layout.cshtml";
  }
  
  @Html.Partial("_NewOrder")
  </code></pre>
  
  <p>In this main view I use a form in a partial view (_NewOrder.cshtml): </p>
  
  <pre><code>&lt;form asp-controller="NewOrder" asp-action="CreateOrder" class="well"&gt;
      &lt;div id="form-horizontal"&gt;
          &lt;div asp-validation-summary="ModelOnly" class="text-danger"&gt;&lt;/div&gt;
          &lt;input type="hidden" id="eventID" class="form-control"&gt;
          &lt;div class="form-group"&gt;
              &lt;label asp-for="Address" class=""&gt;&lt;/label&gt;
              &lt;div class=""&gt;
                  &lt;input asp-for="Address" class="form-control"&gt;
                  &lt;span asp-validation-for="Address" class="text-danger"&gt; &lt;/span&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label asp-for="PhoneNumber" class=""&gt;&lt;/label&gt;
              &lt;div class=""&gt;
                  &lt;input asp-for="PhoneNumber" class="form-control"&gt;
                  &lt;span asp-validation-for="PhoneNumber" class="text-danger"&gt; &lt;/span&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div id="ErrorsBox" class="alert alert-danger" style="display: none;"&gt;&lt;/div&gt;
      &lt;div class="form-group"&gt;
          &lt;div class=""&gt;
              &lt;input type="submit" formnovalidate="formnovalidate" value="Submit new order" class="btn btn-primary" /&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/form&gt; 
  </code></pre>
  
  <p>And of course I have the corresponding controller and HttpPost action method, which is the following:</p>
  
  <pre><code>public class NewOrderController : Controller
  {
      private readonly INewOrderSaverRequestHandler _newOrderSaverRequestHandler;
  
      public NewOrderController(INewOrderSaverRequestHandler newOrderSaverRequestHandler)
      {
          _newOrderSaverRequestHandler = newOrderSaverRequestHandler;
      }
  
      public IActionResult NewOrder()
      {
          return View();
      }
  
      [HttpPost]
      public IActionResult CreateNewOrder(NewOrderViewModel newOrder)
      {
          _newOrderSaverRequestHandler.Handle(newOrder, ModelState);
  
          return RedirectToPage("NewOrder");
      }
  }
  </code></pre>
  
  <p>The issue is that if I click on the submit button, the page is redirected to the following url: "<a href="http://localhost:13078/NewOrder/CreateOrder" rel="nofollow noreferrer">http://localhost:13078/NewOrder/CreateOrder</a>". But what I would like is to stay on the same page, so at "<a href="http://localhost:13078/NewOrder/NewOrder" rel="nofollow noreferrer">http://localhost:13078/NewOrder/NewOrder</a>" url</p>
  
  <p>I have already tried some solutions which I've found here at stackoverflow:</p>
  
  <ul>
  <li>Using <code>return RedirectPage("NewOrder")</code> - with this I am still redirected to .../NewOrder/CreateOrder so to an 404 error page. Furthermore I tried to use it with other cobinations like <code>return RedirectPage("/")</code> or <code>return RedirectPage("NewOrder/NewOrder")</code> but none of them led to the correct solution</li>
  <li>With <code>return View("NewOrder")</code> i am still directed to ".../NewOrder/CreateOrde" as well</li>
  <li>I also tried the following: <code>return Page()</code> but this dependency/reference is unknown in asp.net core 2.0.</li>
  <li>Furthermore i tried to solve my issue with setting the "asp-route-returnurl" attribute in my html form tag, but no success with it.</li>
  </ul>
  
  <p>Well, I think that maybe my issue has something to do with my routing. I use the following routing-setting in my Startup.cs:</p>
  
  <pre><code>    app.UseMvc(routes =&gt;
      {
          routes.MapRoute(
              name: "default",
              template: "{controller=Map}/{action=Index}/{id?}");
      });
  </code></pre>
  
  <p>Last but not least I tried to use annotation routing, but it could also not solve my issue:</p>
  
  <pre><code>    [Route("[controller]/NewOrder")]
      [HttpPost]
      public IActionResult CreateNewOrder(NewOrderViewModel newOrder)
      {
          _newOrderSaverRequestHandler.Handle(newOrder, ModelState);
  
          return RedirectToPage("NewOrder");
      }
  </code></pre>
  
  <p>So what I want is that after clicking the submit button, create a new order (which is handled by <code>INewOrderSaverRequestHandler</code>) then stay on the same page. What do you think, where the problem lies in my code? Thanks in advance!</p>