简体   繁体   中英

ASP.Net MVC - How can a button access url query string parameters?

I'm working on a password reset project.

Users are sent an email with a url like: mysite.com/ResetPass?id=10&token=233rgths567sdsfg

This contains a user Id and JWT token in the url as parameters.

The MVC controller shows the password reset page.

        [HttpGet("ResetPass")]

        public IActionResult ResetPass([FromQuery] int id, [FromQuery] string token)
        {
            // How to send the id and token from the Url query string to the Button???

            return View("ResetPass");
        }


        // Reset Password Button
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> ResetPassButton()
        {
             // How to access the user id and token?
        }

I want to have a form on this page that allows users to change their password then send the new password, user Id and token to the API for processing.

But how do I get the variables from the url (the user id and token) to the button?

Do I just use Viewbag and pass them to the page? Or is there a way for the button to just access the Url query string?

You can create reset password model and pass this model around. Example:

public class ResetPasswordModel
{
    public int Id { set; get; }
    public string Token { set; get; }
}

Then:

    public IActionResult ResetPass([FromQuery] int id, [FromQuery] string token)
    {
        var model = new ResetPasswordModel {
           Id = id,
           Token = token,
        }

        return View("ResetPass", model);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> ResetPassButton(ResetPasswordModel model)
    {
         // do something with model
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM