【问题标题】:Action method to delete a user with ASP.NET Identity 3.x使用 ASP.NET Identity 3.x 删除用户的操作方法
【发布时间】:2016-04-16 07:07:38
【问题描述】:

这是我删除用户的操作方法: 我觉得它是阻塞的,因为我使用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");
}

【问题讨论】:

    标签: c# asp.net asp.net-identity asp.net-core-1.0 asp.net-identity-3


    【解决方案1】:

    你是对的。使用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

    【讨论】:

      猜你喜欢
      • 2018-01-21
      • 2015-03-19
      • 2019-07-02
      • 1970-01-01
      • 2020-12-28
      • 1970-01-01
      • 2014-09-11
      • 2017-06-25
      • 1970-01-01
      相关资源
      最近更新 更多