这是我删除用户的操作方法:
我觉得它正在阻塞,因为我使用 user.Result
将实际用户对象从一个异步结果传递到下一个异步方法。有更好的方法吗?
// POST: Users/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var user = _userManager.FindByIdAsync(id.ToString());
var result = await _userManager.DeleteAsync(user.Result);
return RedirectToAction("Index");
}
最佳答案
你是对的。使用 user.Result
传递实际对象会阻塞异步方法。
使用async
的最佳实践是在整个方法中始终使用await
。 不要混合使用阻塞代码和异步代码。
// POST: Users/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id) {
var user = await _userManager.FindByIdAsync(id.ToString());
var result = await _userManager.DeleteAsync(user);
return RedirectToAction("Index");
}
来源 - Async/Await - Best Practices in Asynchronous Programming
关于c# - 使用 ASP.NET Identity 3.x 删除用户的操作方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36661236/