【问题标题】:Value cannot be null error in processing OData from Web API处理来自 Web API 的 OData 时值不能为空错误
【发布时间】:2014-06-09 08:07:18
【问题描述】:

我有一个返回 OData 的 WebAPI 方法:

[HttpGet]
public PageResult<Students> GetStudents(ODataQueryOptions<Students> queryOptions, string ClassId)
    {
        var allStudents = (from s in new OStudentContext(Convert.ToInt64(ClassId)).Student select s).ToList();

        var results = queryOptions.ApplyTo(allStudents.AsQueryable());

        return new PageResult<Students>(results as IEnumerable<Students>, Request.GetNextPageLink(), Request.GetInlineCount());

    }

请求网址是这样的:

http://localhost:3333/api/odata/GetStudents?StudentId=40932&$inlinecount=allpages&$filter=((IsDeleted%20eq%20null)or(IsDeleted%20eq%20false))&$select=StudentId,FirstName,LastName,EmailID

结果中的值在那里,我可以看到返回的记录。准确地说,计数实际上是 7。

问题是return语句抛出了这个异常:

值不能为空。参数名称:数据

Request.GetNextPageLink() 为空,因为没有下一页链接。

Request.GetInlineCount() 为 7。

那么什么是 null 以及错误对数据意味着什么?

提前致谢。

【问题讨论】:

    标签: odata asp.net-web-api2


    【解决方案1】:

    如果有$select或$expand,应用ODataQueryOptions后的元素类型不再是Students。

    改为 SelectExpandWrapper。

    所以结果不能转换为IEnumerable&lt;Students&gt;

    SelectExpandWrapper的结构可以参考https://aspnetwebstack.codeplex.com/wikipage?title=%24select%20and%20%24expand%20support

    【讨论】:

      猜你喜欢
      • 2013-10-23
      • 1970-01-01
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      • 2021-01-14
      • 2015-12-05
      • 2014-03-28
      • 2013-09-13
      相关资源
      最近更新 更多