本文介绍了限制OData如何导致WebAPI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently developing an application which uses a Web API to return data which is stored in a database.

现在,我需要能够过滤结果,因此将OData过滤到请求中,这样就可以在查询字符串中构建整个过滤器.

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:

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);
}

正在从WebApiConfig.cs文件中调用此方法.

This method is being called from within the WebApiConfig.cs file.

Global.asax文件确实包含以下代码:

The Global.asax file does contain the following code:

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;
 }

因此,该方法带有EnableQuery属性标记,并且控制器也确实继承了ODataController.

So, the method is marked with the EnableQuery attribute and the controller does also inherit from ODataController.

现在,我可以查询一些包含数百万条记录的表,当然,我什至不希望GetAll找到通往数据库的方式.

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.

因此,我坚信在WebApiConfig文件中,以下代码就足够了:

Therefore, I tought that in the WebApiConfig file, the following code was sufficient:

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

但是,这不限制结果,但是当我请求带Take过滤器的URL大于此处定义的MaxTop值时,会引发错误.

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.

有没有一种方法可以确保即使我请求的URL没有任何过滤器参数,结果也将仅包含启用分页的前'x'个记录?

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?

推荐答案

请尝试使用服务器分页选项来过滤记录,以防万一您想限制获取记录的数量.

Please try server paging option to filter the records in case if you want to restrict the number of records fetching.

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

这篇关于限制OData如何导致WebAPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 20:09