callbackUrl正在改变?与%3f

时间:2016-04-29 10:32:38

标签: asp.net-mvc

我目前正在开展一个项目并遇到一个奇怪的问题, 我生成一个callbackUrl进入确认电子邮件,以激活注册帐户:

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

问题是它生成了这个网址:

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

你可以看到它似乎取代了?与%3f我希望这与html编码有关。 我试过追加?对于confirmToken,但这给了我测试结果。

我目前正在使用ActionMailer软件包以最小化我的控制器中的代码,当收到电子邮件时它似乎没有正确解码callbackUrl,而当我直接在我的Register HttpPost中使用电子邮件逻辑时它解码得很好。

EmailController:

    public class EmailController : MailerBase
    {
                public EmailResult ConfirmationEmail(Register model, string callbackUrl)
                {
                    From = "donotreply@kevindefrene.me";
                    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");
    }

电子邮件视图:

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

2 个答案:

答案 0 :(得分:1)

当您使用Url.Action("A","B",new{a="x"})时,它会自动创建类似“/ B / A?a = x”的查询字符串。所以你不需要添加?在那里标记。有关更多相关信息,请查看MSDN参考。

答案 1 :(得分:1)

删除&#34;?&#34;。使用以下代码:

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

所以你的网址是 - http://localhost:8159/account/registerconfirmation?Id=fv9egt7cjoEGQvHfauiYE1A