从2015年1月起应用EnableQueryAttribute哪里正确/不正确?
说:
The [EnableQuery] attribute enables clients to modify the query,by using query options such as $filter,$sort,and $page. For more information,see Supporting OData Query Options.
http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options
说:
The EnableQuerySupport method enables query options globally for any controller action that returns an IQueryable type.
但是关于WebApi 2.2上的OData 4的这个文档已经将它放在了返回IHttpActionResult的操作上:
[ODataRoutePrefix("Teams")] public class TeamsEntitySetController : ODataController { private readonly LeageContext _leage = new LeageContext(); [EnableQuery] [ODataRoute] public IHttpActionResult GetFeed() { return Ok(_leage.Teams); } [ODataRoute("({id})")] [EnableQuery] public IHttpActionResult GetEntity(int id) { return Ok(SingleResult.Create<Team>(_leage.Teams.Where(t => t.Id == id))); } }
我会疯狂地尝试在OData v4 / WebApi 2.2上找到最新,准确和一致的文档.
今天是正确的?
解决方法
使用全局配置(HttpConfiguration对象的实例)并调用
config.Filters.Add(new EnableQueryAttribute() { PageSize = 2 // .. other settings });
这个工作