Url.Action()导致System.AccessViolationException

时间:2015-08-25 13:35:06

标签: c# asp.net-mvc asp.net-identity-2

我在标准ASP.NET MVC项目中向ASP.NET身份模型添加了电子邮件验证。以下行导致AccessViolationException:

callbackUrl = Url.Action("ConfirmEmail", "Account", confirmModel, Request.Url.Scheme);

更新: 无法解决的问题是它消失了。我会试着弄清楚是什么让它消失了。令我担心的是,我不知道解决方案有任何重大变化。

注册用户的完整帐户控制器方法如下:

// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        ApplicationUser user = new ApplicationUser { UserName = model.Email, Email = model.Email };
        IdentityResult result = await UserManager.CreateAsync(user, model.Password);
        if (result.Succeeded)
        {
            string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

            string callbackUrl;
            try
            {
                string requestScheme = Request.Url.Scheme;
                object confirmModel = new { userId = user.Id, code = code };
                callbackUrl = Url.Action("ConfirmEmail", "Account", confirmModel, Request.Url.Scheme); // TODO: Fails somehow!
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                callbackUrl = "https://localhost:43000/Account/ConfirmEmail?userid=" + user.Id + "&code=" + code;
            }

            await
                UserManager.SendEmailAsync(
                        userId: user.Id,
                        subject: "Verify it's you",
                        body: "Please confirm your email address by clicking <a href=\"" + callbackUrl + "\">here</a>");

            return View("CheckYourEmail");
        }

        AddErrors(result);
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

不幸的是,没有内部异常或任何有用的东西。

catch()块解决了问题的解决方法 但我真的很好奇这里出了什么问题。

1 个答案:

答案 0 :(得分:0)

使用&#34; dynamic confirmModel = ...&#34;而不是设置对象confirmModel = ...看看是否能修复它。