Url.Action()引起了System.AccessViolationException。

5
我在标准的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()块作为一种解决方法可以解决这个问题。
但我真的很好奇这里出了什么问题。


你能复制并粘贴ex.ToString()的完整输出文本到你的问题中吗? - Scott Chamberlain
异常的堆栈跟踪是什么样子的? - lc.
1个回答

0

不要设置对象confirmModel = ...,而是使用"dynamic confirmModel = ...",看看是否可以解决问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接