按键查询时,SingleResult <T>在Web API中不可序列化

Ste*_*eve 4 wcf-data-services odata asp.net-web-api asp.net-web-api-odata

尝试使用主键CourseID针对odata web.api查找单个记录,方法如下:

var editedcourse = container.Courses.Where(c => c.CourseID == ID).SingleOrDefault();
Run Code Online (Sandbox Code Playgroud)

这是错误的:

    <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>
Run Code Online (Sandbox Code Playgroud)

Ste*_*eve 5

默认情况下,web.api控制器方法不可查询,因此客户端失败。添加了注释以修复: [Queryable(AllowedOrderByProperties = "Id")]

  • 也为我修复了该问题,但我想了解原因!?为什么必须要查询才能正确序列化它? (2认同)