期待將身份驗證添加到MVC 5 Boilerplate template,下一段代碼在其自己的original sample project中運行良好,但將其內容集成到Boilerplate模板後,並試圖註冊一個新用戶,成爲一些相互矛盾的,出現一個瀏覽器異常,指着下面「的await」行:嘗試將身份驗證添加到ASP.NET MVC Boilerplate模板 - 異步/等待問題
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
string callbackUrl = await SendEmailConfirmationTokenAsync(user.Id, "Confirm your account");
ViewBag.Message = "Check your email and confirm your account, you must be confirmed "
+ "before you can log in.";
ViewBag.Link = callbackUrl;
return View("Info");
}
AddErrors(result);
}
return View(model);
}
我已經在很多地方讀到,當一個異步的問題發生,人們建議,使之同步的,但我的情況下,許多事情會變得incompatible.I不知道如何保持這種方法異步因爲它的淵源in the template,
首先,這是一個警告,而不是一個錯誤。它並不妨礙你的應用程序工作,這只是一個跡象,你正在做一些奇怪的事情。其次,它是完全正確的 - 你的'GetItems'方法沒有任何'await's,所以聲明中的'async'關鍵字是無用的,並且該方法將是100%同步的 - 沒有理由返回一個'任務'要麼。 – Luaan
瀏覽器提供了一個例外,我不能忽視這一點,請閱讀整個問題,包括模板討論。 –
您是否將警告編譯爲錯誤? – Lee