6

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.

2
  • What do you mean by 'are lost'? How does your controller look? Posting a form and query string parameters are not really related to each other, they're just part of the URL that sends a POST request to the server. Commented Oct 27, 2016 at 20:25
  • When the Account/Login route is loaded for first time there is query string parameter. when the form is submitted query string url no longer exists. If I remember correctly previous version of mvc used to retain the query string parameter when using Html.BeginForm helper methods. Commented Oct 27, 2016 at 20:50

4 Answers 4

10

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I wanted to add that this didn't directly work with: @Request.QueryString. So if Request Doesn't exist you might want to try: @Context.Request.QueryString, this worked for me
1

I know that it's passed a lot of time, but I found a better solution for this problem.

I added a parameter called QueryString in Model as Dictionary string

in view, in tag form, add

So at this time, the post have the parameters in query string<form asp-all-route-data="@Model.QueryString"

1 Comment

Including a dictionary in every view model seems impractical but minimally declaration and server side code to retrieve values is necessary to even attempt.
0

Your controller/PageModel method must contain all parameters that you need to persist. Something like this:

public async Task<IActionResult> OnPostAsync(string returnUrl = null)

1 Comment

That's for a Razor Pages app. In MVC we don't have the OnPostAsync concept.
0

If the controller and action pair for getting and posting the form is the same, than it is simpler to just delete asp-controller and asp-action attributes from the form opening tag, leaving your like this:

<form class="c-form" method="post">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.