简体   繁体   English

限制 OData 如何导致 WebAPI

[英]How limit OData results in a WebAPI

I'm currently developing an application which uses a Web API to return data which is stored in a database.我目前正在开发一个应用程序,它使用 Web API 返回存储在数据库中的数据。

Now, I need to be able to filter the results, so OData to the request, that way I can built an entire filter in a query string.现在,我需要能够过滤结果,所以 OData 到请求,这样我就可以在查询字符串中构建整个过滤器。

Here's the configuration of the 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);
}

This method is being called from within the WebApiConfig.cs file.正在从WebApiConfig.cs文件中调用此方法。

The Global.asax file does contain the following code: Global.asax文件包含以下代码:

protected void Application_Start()
{
    GlobalConfiguration.Configure(WebApiConfig.Register);
    GlobalConfiguration.Configuration.EnsureInitialized();

    UpdateDatabaseToLatestVersion();
}

The controller that I use to query the data is the following:我用来查询数据的控制器如下:

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

     return data;
 }

So, the method is marked with the EnableQuery attribute and the controller does also inherit from ODataController .因此,该方法标有EnableQuery属性,并且控制器也继承自ODataController

Now, there are some tables which I can query which contains millions of records, and off course, I don't even want a GetAll to find it's way to the database.现在,我可以查询一些包含数百万条记录的表,当然,我什至不希望 GetAll 找到通往数据库的路。

Therefore, I tought that in the WebApiConfig file, the following code was sufficient:因此,我认为在WebApiConfig文件中,以下代码就足够了:

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

However, this does not limit the results, but it throws an error when I request the url with a take filter which is more than the MaxTop value defined here.但是,这不会限制结果,但是当我请求带有超过此处定义的 MaxTop 值的 take 过滤器的 url 时,它会引发错误。

Is there a way to ensure that, even if I request the url without any filter parameters, that the result will only contain the first 'x' records with paging enabled?有没有办法确保,即使我请求没有任何过滤器参数的 url,结果也将只包含启用分页的第一个“x”记录?

Please try server paging option to filter the records in case if you want to restrict the number of records fetching.如果您想限制获取记录的数量,请尝试使用服务器分页选项来过滤记录。

Check this link - http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options#server-paging检查此链接 - http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options#server-paging

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM