【发布时间】:2016-08-29 11:19:04
【问题描述】:
我是带有 Web API 2 的 ASP.NET MVC 的新手,我通过使用带有 Web API 2 的 asp.net MVC 创建了 async 和 await 方法,该方法将存储到 SQL DB 中。当我尝试在 API 控制器中调用我的存储库方法时,我收到一个错误,无法等待“system.collections.generic.list”。如果有人对此有任何想法,请告诉我。
注意:- 我不想使用实体框架,而是想通过存储过程存储数据。
Model:
namespace AsyncMVCWEBAPI.Models
{
public class Product
{
public string Name { get; set; }
public double Price { get; set; }
public string Category { get; set; }
}
}
API Controller:
public static async Task<Product> GetProduct()
{
return await GetAllProduct(); // Here i'm getting an error can not await "system.collections.generic.list"
}
Repository:
public static IEnumerable<Product> GetAllProduct()
{
using (SqlConnection con = new SqlConnection(connectionString))
{
if (con.State == ConnectionState.Closed)
con.Open();
List<Product> students = new List<Product>();
SqlCommand cmd = new SqlCommand("spGetAllStudentDetails", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Product product = new Product();
product.Price = Convert.ToInt32(rdr["Price"]);
product.Name = rdr["Name"].ToString();
product.Category = rdr["Category"].ToString();
students.Add(product);
}
return students;
}
}
【问题讨论】:
标签: c# async-await asp.net-web-api2 c#-5.0 asp.net-mvc-5.1