C# 限制OData如何产生WebAPI

C# 限制OData如何产生WebAPI,c#,asp.net-mvc-4,asp.net-web-api,C#,Asp.net Mvc 4,Asp.net Web Api,我目前正在开发一个应用程序,它使用Web API返回存储在数据库中的数据 现在,我需要能够过滤结果,以便OData能够响应请求,这样我就可以在查询字符串中构建一个完整的过滤器 以下是OData的配置: private static void ConfigureOData(HttpConfiguration config) { ODataModelBuilder builder = new ODataConventionModelBuilder(); builder.EntityS

我目前正在开发一个应用程序,它使用Web API返回存储在数据库中的数据

现在,我需要能够过滤结果,以便OData能够响应请求,这样我就可以在查询字符串中构建一个完整的过滤器

以下是OData的配置:

private static void ConfigureOData(HttpConfiguration config)
{
    ODataModelBuilder builder = new ODataConventionModelBuilder();
    builder.EntitySet<GenericArticle>("GenericArticle");
    config.MapODataServiceRoute(routeName: "ODataRoute", routePrefix: "OData", model: builder.GetEdmModel());

    // Defines the configuration for OData.
    var queryAttribute = new QueryableAttribute
    {
        MaxTop = Convert.ToInt32(ConfigurationManager.AppSettings["OData:MaxTop"]),
        PageSize = 2
    };

    config.EnableQuerySupport(queryAttribute);
}
我用于查询数据的控制器如下所示:

 [EnableQuery]
 public IQueryable<GenericArticle> Get()
 {
     var data = UnitOfWork.GenericArticlesRepository.Entities;

     return data;
 }
但是,这并没有限制结果,但当我使用take筛选器请求url时会抛出一个错误,该筛选器大于此处定义的MaxTop值


是否有办法确保,即使我请求的url没有任何筛选参数,结果也只包含启用分页的第一个“x”记录?

如果要限制获取记录的数量,请尝试使用服务器分页选项筛选记录


查看此链接-

谢谢您的回答。我知道我可以通过在查询中指定它来实现,但我认为有一个通用的地方可以定义它(无需编写自定义属性)。
 [EnableQuery]
 public IQueryable<GenericArticle> Get()
 {
     var data = UnitOfWork.GenericArticlesRepository.Entities;

     return data;
 }
var queryAttribute = new QueryableAttribute
{
    MaxTop = Convert.ToInt32(ConfigurationManager.AppSettings["OData:MaxTop"]),
    PageSize = 2
};