asp.net core mvc application form post with query string parameters

asp.net-core, asp.net-mvc

Solution

To keep the query string when the form is submitted write a hidden field in the form containing the query string contents:

@Html.Hidden("returnUrl",@Request.QueryString)

Make sure your controller action that handles the post request has a parameter called returnUrl (or the model that is passed as a parameter has that property) and the model binding will take care of passing it through to the controller. Then in the controller action if the login is successful use that data to redirect accordingly.

Problem

I am building a login form in .net core mvc. Below is my login form ``` <form class="c-form" asp-controller="Account" asp-action="Login"> <div class="form-group"> @Html.TextBoxFor(m => m.Username, new { @class = "form-control c-input", placeholder = "Username" }) </div> <div class="form-group"> @Html.PasswordFor(m => m.Password, new { @class = "form-control c-input", placeholder = "Password" }) </div> <div class="help-text help-text-error"> @Html.ValidationMessage("UserNamePasswordInvalid") </div> <div class=""> <button type="submit" class="btn-c btn-teal login-btn width100">Login</button> </div> ``` If a form is posted with incorrect credentials user stays on the page with validation failure messages. Login page also has return url in query string, when the form is posted query string parameters are lost. What is the correct way of doing form post in .net core.

Original source