josephzigler
josephzigler

Reputation: 285

callbackUrl changing ? with %3f

I'm currently working on a project and experiencing a strange problem, I generate a callbackUrl that goes into the confirmation email in order to activate the registred account:

var callbackUrl = Url.Action("registerconfirmation", "account", new { Id = "?" + confirmationToken }, protocol: Request.Url.Scheme);

The problem is that it generates this url:

http://localhost:8159/account/registerconfirmation/%3fv9egt7cjoEGQvHfauiYE1A

As you can see it seems to replace the ? with %3f I expect this has something to do with html encoding. I tried appending the ? to the confirmationToken but that gave me tesame result.

I am currently using the ActionMailer package in order to minimize my code in my controllers, when recieving the email it does not seem to decode the callbackUrl properly, while when I had the email logic directly in my Register HttpPost it decoded fine.

EmailController:

    public class EmailController : MailerBase
    {
                public EmailResult ConfirmationEmail(Register model, string callbackUrl)
                {
                    From = "[email protected]";
                    To.Add(model.Email);
                    Subject = ("Email Confirmation");
                    ViewBag.Url = callbackUrl;
                    return Email("ConfirmationMessage", model);
                }
    }

RegisterController:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Register(Register model)
    {
        string confirmationToken = CreateGuid();
        Account account = new Account(model.Username, model.Password, model.FirstName, model.LastName, model.Email, false, confirmationToken);
        Session["accountID"] = Repository.InsertAccount(new Account(model.Username, model.Password, model.FirstName, model.LastName, model.Email, false, confirmationToken));
        var callbackUrl = Url.Action("registerconfirmation", "account", new { Id = "?" + confirmationToken }, protocol: Request.Url.Scheme);
        try
        {
            new EmailController().ConfirmationEmail(model, callbackUrl).Deliver();
        }
        catch (Exception e)
        {
            ModelState.AddModelError("ConfirmationMailFailed", "Problem sending email" + e.Message);
        }
        return View("emailconfirmation");
    }

Email View:

<p>To get started, please click <a href="@ViewBag.Url">here</a> to activate your account.</p>

Upvotes: 2

Views: 1352

Answers (2)

Karthik M R
Karthik M R

Reputation: 990

Remove "?". Use the below code:

var callbackUrl = Url.Action("registerconfirmation", "account", new { Id = confirmationToken }, protocol: Request.Url.Scheme);

So your url would be - http://localhost:8159/account/registerconfirmation?Id=fv9egt7cjoEGQvHfauiYE1A

Upvotes: 1

SamGhatak
SamGhatak

Reputation: 1493

When you use Url.Action("A","B",new{a="x"}), it automatically creates the querystring like "/B/A?a=x". So you don't need to add the ? mark there. For more relevant information check the MSDN reference.

Upvotes: 2

Related Questions