【发布时间】:2014-05-10 23:16:52
【问题描述】:
尝试使用主键 CourseID 针对 odata web.api 查找单个记录:
var editedcourse = container.Courses.Where(c => c.CourseID == ID).SingleOrDefault();
这是错误:
<m:innererror>
<m:message>The 'ObjectContent`1' type failed to serialize the response body for content type 'application/atom+xml; charset=utf-8'.</m:message>
<m:type>System.InvalidOperationException</m:type>
<m:stacktrace></m:stacktrace>
<m:internalexception>
<m:message>'SingleResult`1' cannot be serialized using the ODataMediaTypeFormatter.</m:message>
<m:type>System.Runtime.Serialization.SerializationException</m:type>
【问题讨论】:
-
我在 Web API 2.2 和 6.5.0 的 OData 库中遇到了同样的异常。当我的操作未枚举任何结果时会发生这种情况,即用户使用未实现实体的键进行查询。您接受的关于 QueryableAttribute 的答案在最新版本中不起作用,有什么建议吗?我也遇到过this closed ticket。
-
@AdamCaviness 我遇到了同样的问题。此链接:aspnetwebstack.codeplex.com/workitem/1040 似乎暗示它实际上应该返回 404 而不是抛出异常 - 它没有。所以我想出了一个解决方法:gist.github.com/andygjp/82106facbb0c43f55dc8。希望它有所帮助(直到他们修复它)。
标签: odata wcf-data-services asp.net-web-api asp.net-web-api-odata