Not all values of `Url.Page(values)` show up in the Get method of the page model

95 Views Asked by At

The following code builds a clickable URL which is emailed to user:

var callbackUrl = Url.Page(
    "/Account/ConfirmEmail",
    pageHandler: null,
    values: new { userId = response.Id, code = response.EmailConfirmationCode, returnUrl = returnUrl },
    protocol: Request.Scheme);

All the params / values show up in the clickable URL in the email. However, when the user clicks the URL, the page model Get method only gets the first userId param:

public async Task<IActionResult> OnGetAsync(string userId, string code, string returnUrl)
{
 //
}

I have tried public async Task<IActionResult> OnGetAsync([FromQuery] string userId, [FromQuery] string code, [FromQuery] string returnUrl) but to no avail.

HttpContext.Request.Query is empty too. What do I miss?

2

There are 2 best solutions below

0
Mike Brind On

Your generated URL is getting HTML-encoded at some point in the process and the query string & separators are converted to their equivalent HTML entity (&amp;). This is fine if you want to display the query string in an HTML page, but not if you want to read the query string.

If you are using Razor at any point to generate the HTML email, use Html.Raw() on the URL to prevent it from being encoded.

You should find that https://iam.mydomain.com/Account/ConfirmEmail?userId=<GUID>&code=<very long string>&returnUrl=%2F_Host works as expected.

0
Kok How Teh On

Use the public Task SendMailAsync(MailMessage message); variant of the interface setting IsBodyHtml to true solves the problem.