【问题标题】:How to format iqueryable mvc 4 web api requests?如何格式化 iqueryable mvc 4 web api 请求?
【发布时间】:2013-04-17 06:50:49
【问题描述】:

我想在我的所有集合上使用 iqueryable,以便获得所有 odata 功能。但是我需要使用以下字段格式化请求的响应;

{
   href: "url to resouce",
   length: 10,
   items: [
     "(IQueryable results)"
   ]
}

格式化响应不是困难的部分,但保持所有 odata “东西”正常工作是。

所以我的代码看起来像;

MyFormattedResponse.Href = "poop.com";
MyFormattedResponse.Length = 0;
MyFormattedResponse.Items = _myRepo.Gets();
Request.CreateResponse(HttpStatusCode.OK, MyFormattedResponse);

但我得到的错误是:

控制器“MyResource”上的操作“Get”,返回类型 'MyObject' 不支持查询。确保返回的类型 content 是 IEnumerable、IQueryable 或两者的通用形式 界面。

这是我可以在媒体格式化程序或过滤器中构建的东西吗?

我真的很想保持 odata 的魅力......

【问题讨论】:

    标签: asp.net-mvc-4 asp.net-web-api odata iqueryable


    【解决方案1】:

    看看我提供的这个答案:

    Web API Queryable - how to apply AutoMapper?

    你的情况类似,你可以这样做:

    public MyFormattedResponse Get(ODataQueryOptions<Person> query)
    {
        var results = query.ApplyTo(_myRepo.Gets()) as IEnumerable<Person>;
        return new MyFormattedResponse() { Href = "foo.com", Length = 5, Items = results };
    }
    

    确保删除 [Queryable] 属性。

    【讨论】:

      猜你喜欢
      • 2020-03-05
      • 1970-01-01
      • 1970-01-01
      • 2020-02-20
      • 1970-01-01
      • 2020-11-07
      • 2012-09-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多