我在标准的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