【发布时间】:2016-01-20 22:41:24
【问题描述】:
我在一个返回 SingleResult 的 oData V4 项目中有一个控制器方法。它查询一个表,当它有数据时,它会返回它应该返回的内容。但是,当应用程序是新的并且表为空时,它会给出一个无法描述的错误:
"'SingleResult`1' cannot be serialized using the ODataMediaTypeFormatter."
我的方法如下所示:
[EnableQuery]
public virtual SingleResult<LoggerEntry> Get([FromODataUri] int key)
{
using (var gateway = this.Gateway.GetChild())
{
var model = gateway.GetLoggerEntry(key);
// TODO: Check for null and tell the client NotFound
return SingleResult.Create((new List<LoggerEntry>(){ model }).AsQueryable());
}
}
因为我使用的是 SingleResult,所以我不能只使用 return NotFound();。我试图使用一个空列表,但这给了我同样的错误。在这种情况下,我找不到要返回的示例或文档。我应该返回什么。
【问题讨论】:
标签: asp.net odata asp.net-web-api2