我正在尝试使用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<>
发布于 2013-05-08 02:24:27
您只能使用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);
}
}https://stackoverflow.com/questions/16423922
复制相似问题