【发布时间】:2015-09-24 02:45:00
【问题描述】:
找不到行时如何返回404?但是,以下代码在return NotFound() 行出现错误。
错误 1 无法将类型“System.Web.Http.Results.NotFoundResult”隐式转换为“System.Collections.Generic.IEnumerable
”。存在显式转换(您是否缺少演员表?)
public IEnumerable<Product> GetProductsByReportId(int rid)
{
using (var db = new MyContext())
{
var query = from b in db.table where b.rid == rid select b;
if (query == null)
{
return NotFound(); // Error!
}
return query.ToList();
}
}
【问题讨论】:
-
值得注意的是,查询永远不会为空——它是一个定义。查询的结果也永远不会为空——它将是空的。在解决主要问题时检查是否正确。
标签: c# asp.net asp.net-web-api