ASP.net MVC 4(Web api)OData 配置
一直在使用(单页应用程序)BigShelf 示例。我发现真正有趣的是 GetBooksForSearch 方法 (/api/BigShelf/GetBooksForSearch),它需要额外的 $filter
、$inlinecount
、$top
、用于分页和过滤结果的 $skip
参数,这些参数在控制器代码中不存在:
public IQueryable<Book> GetBooksForSearch
(string profileIds, Sort sort, bool sortAscending)
我找不到任何关于控制器如何翻译和过滤结果的文档,更重要的是,如何配置这样的行为(例如,限制最大结果),有人知道吗?
-- 更新 --
发现 MVC Web API 正在发挥作用。但我们该如何配置呢?
Been playing around with the (Single Page App) BigShelf sample. I found really interesting is the GetBooksForSearch method (/api/BigShelf/GetBooksForSearch) that it takes additional $filter
, $inlinecount
, $top
, $skip
parameters for paging and filtering results, which are not present in the controller code:
public IQueryable<Book> GetBooksForSearch
(string profileIds, Sort sort, bool sortAscending)
I coudln't find any documents about how that Controller translate and filter the result afterwards and more importantly, how to configure such behavior (e.g., limit the max result), anyone have a clue?
-- Updated --
Found out that MVC Web API is doing the trick. But how can we configure it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ResultLimitAttribute 似乎已被删除。请参阅此 commit
它已被纳入 [Queryable] 属性的一项功能中,现在这是 OData 支持所必需的。请参阅此处的讨论,
正确的用法现在是
[Queryable(ResultLimit = 10)]
[更新]
自 RTM 起,Queryable 的 ResultLimit 功能已被删除。此外,[Queryable] 已移至其自己的预览包中。请参阅此博客文章了解更多信息和此帖子获取说明关于新的用法。
[更新 2 11-16-12]
随着 ASP.Net 2012 年秋季更新预览的出现,一切都得到了再次更新。
[Queryable] 属性的 ResultLimit 属性已添加回 OData 包。
请参阅文章 此处了解一些更改的概要。
此处是更新后的 Nuget 包。截至撰写本文时,它是一个 PREVIEW 包。
It appears that the ResultLimitAttribute has been removed. See this commit
It was rolled into a feature of the [Queryable] attribute which is now required for OData support. See discussion here
Proper Usage would now be
[Queryable(ResultLimit = 10)]
[UPDATE]
As of RTM the ResultLimit feature of Queryable has been removed. Also, [Queryable] has been moved to it's own preview package. See this blog post for more information and this post for instructions on the new usage.
[UPDATE 2 11-16-12]
With the ASP.Net Fall 2012 Update Preview things have been updated again.
The ResultLimit Property of The [Queryable] attribute has been added back to the OData package.
See article here for a run down of some of the changes.
Here is the updated Nuget package. As of this writing it is a PREVIEW package.
有一个名为 的操作筛选器属性
ResultLimitAttribute
您可以在任何返回IQueryable
甚至返回的操作方法上使用它IEnumerable
用于限制返回的数据量。There's an action filter attribute called
ResultLimitAttribute
which you can use on any action method which returnsIQueryable<T>
or evenIEnumerable<T>
to limit the amount of data returned.有一个关于使用 MVC Web API 进行分页和查询的简短网络广播,您可以观看 此处
它解释了如何通过
pageIndex
和pageSize
,然后返回请求的项目数。AFAIK,您无法使用第一种方法配置返回的最大项目数,但您可以使用第二种方法限制返回的项目数,只需检查
if (pageSize>maxPage) then return maxPage items.
There is a short webcast about paging and querying with MVC Web API that you can watch here
It explains how you can perform paging by either
pageIndex
andpageSize
, and then returns the requested number of items.AFAIK, you cannot configure the maximum number of items returned by using the first method, but you could limit the number of items returned by using the second approach, and just checking
if (pageSize>maxPage) then return maxPage items
.正如科迪·克拉克(Cody Clark)指出的那样,随着时间的推移,这个领域发生了相当多的变化。从 WebAPI 5.2 版本开始,您现在使用
EnableQueryAnnotation
并使用PageSize
参数,而不是QueryableAttribute
或ResultLimit
>。 ([Queryable]
仍然有效,但它被标记为已过时。)当前您将使用以下语法:通过使用
PageSize
,您可以设置未参数化的默认页面大小请求。如果您不包含MaxTop
值,恶意客户端可能会将顶部设置为非常高的值并绕过页面默认值。使用MaxTop
,如果客户端请求的记录数超出 API 支持的记录数,您将引发异常。As Cody Clark pointed out, this area has seen quite a number of changes over time. As of the 5.2 version of WebAPI, you now use the
EnableQueryAnnotation
and use thePageSize
parameter rather thanQueryableAttribute
orResultLimit
. ([Queryable]
will still work, but it is marked as obsolete.) Currently you would use the following syntax:By using
PageSize
, you set the default page size for unparameterized requests. If you don't include theMaxTop
value, a rogue client could set the top to a very high value and bypass the page defaults. WithMaxTop
, you will throw an exception if the client requests more records than your API supports.