2013-05-07 69 views
0

我正在尝试使用EntitySetController实现自定义分页。如何使用EntitySetController自定义分页

public class MyController : EntitySetController<Poco, int> 
{ 
    public IQueryable<Poco> Get() 
    { 
     var result = _myBusinessLogic.Search(QueryOptions.Top.Value); 
     return result.AsQueryable() 
    } 
} 

我想我失去了一些东西,因为它看起来像控制器尝试应用分页已经只返回一个页面的Search方法的结果。我怎样才能防止它做到这一点,并自己应用分页?

它看起来像我刚刚从ODataController继承,而不是和实施:

public IEnumerable<Poco> Get(ODataQueryOptions odataQueryOptions) 

但我想知道如果我能留在EntitySetController让有较少的管道代码编写。

我想坚持的OData格式,而不是返回PageResult<>

回答

1

可以使用ODataQueryOptions只需要查询的完全控制或让框架完全处理它,你使用QueryableAttribute的。不幸的是没有中间地带。

所以,我认为做一个ODataController是现在解决这个问题的正确方法。

这就是说,我可以建议一个肮脏的解决方法,暂时工作。请注意,这依赖于可能/将会改变并破坏你的内部实现。

public class MyController : EntitySetController<Poco, int> 
{ 
    public IQueryable<Poco> Get() 
    { 
     var result = _myBusinessLogic.Search(QueryOptions.Top.Value); 
     RemoveQueryString(Request, "$top"); 
     return result.AsQueryable() 
    }  

    // This method relies that code that looks for query strings uses the extension 
    // method Request.GetQueryNameValuePairs that relies on cached implementation to 
    // not parse request uri again and again. 
    public static void RemoveQueryString(HttpRequestMessage request, string name) 
    { 
     request.Properties[HttpPropertyKeys.RequestQueryNameValuePairsKey] = request.GetQueryNameValuePairs().Where(kvp => kvp.Key != name); 
    } 
}