【问题标题】:how to write async web api for get method in web api 2?如何在 web api 2 中为 get 方法编写异步 web api?
【发布时间】:2020-01-27 12:02:44
【问题描述】:

显示“此异步方法缺少等待”的警告。请建议在我的异步方法中添加等待的位置?

 public async Task<IHttpActionResult> GetEmployeeDetails(string id,string StartDate, string EndDate)
        {
            DateTime sd = Convert.ToDateTime(StartDate);
            DateTime ed = Convert.ToDateTime(EndDate);
            using (EmployeeEntities db = new EmployeeEntities ())
            {
                try
                {
                    return Ok(db.employees.Where(x => x.TimeStamp >= sd && x.TimeStamp <= ed && x.DeviceImei ==id).OrderByDescending(x => x.id).ToListAsync());
                }
                catch (Exception)
                {
                    return BadRequest("Sorry Error Found!!!");
                }
            }
        }

【问题讨论】:

    标签: asp.net-web-api2


    【解决方案1】:

    你可以在你使用ToListAsync()的return语句中使用它

    return Ok(await db.employees.Where(x => x.TimeStamp >= sd && x.TimeStamp <= ed && x.DeviceImei ==id).OrderByDescending(x => x.id).ToListAsync());
    

    【讨论】:

      猜你喜欢
      • 2020-07-19
      • 1970-01-01
      • 2015-03-30
      • 2020-03-10
      • 1970-01-01
      • 2016-08-30
      • 1970-01-01
      • 2014-05-15
      • 2015-07-22
      相关资源
      最近更新 更多