Search code examples
c#asp.net-coreroutesasp.net-core-mvc.net-8.0

In ASP.NET Core project the page url changes after form submit


I am developing a project in .NET 8 version so I customized page urls in my language.

While submitting the form it works ok but when I return the model to view my page's url changes to my action name.

The default page url (route) that I have form.

https://localhost:7135/article-settings/chosee-author/3/

This is the form I submitted.

<form id="createForm" asp-controller="Settings" asp-action="AddNewAuthor" method="post">
    <input class="form-control mb-1" type="hidden" autocomplete="off" asp-for="ArticleId" />
</form>

And this is the page url after form submitted

https://localhost:7135/article-settings/save-author/

All actions have different route templates, maybe that can be problem but if I don't add custom route templates request can't find the action.

So don't want to change my page url after submit.

Thanks for your help.


Solution

  • I found a method here https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState

     <script>
         var pageUrl = '@Url.Action("Index", "Home")';
         history.replaceState(null, title, pageUrl);
     </script>
    

    This method allows you to change your page url and reload page without re-submitting your form.