ASP.net MVC 4 (web api) OData Configuration

前端 未结 4 1574
半阙折子戏
半阙折子戏 2021-01-12 17:23

Been playing around with the (Single Page App) BigShelf sample. I found really interesting is the GetBooksForSearch method (/api/BigShelf/GetBooksForSearch) that it takes ad

相关标签:
4条回答
  • 2021-01-12 17:38

    It appears that the ResultLimitAttribute has been removed. See this commit

    It was rolled into a feature of the [Queryable] attribute which is now required for OData support. See discussion here

    Proper Usage would now be

    [Queryable(ResultLimit = 10)]

    [UPDATE]

    As of RTM the ResultLimit feature of Queryable has been removed. Also, [Queryable] has been moved to it's own preview package. See this blog post for more information and this post for instructions on the new usage.

    [UPDATE 2 11-16-12] With the ASP.Net Fall 2012 Update Preview things have been updated again. The ResultLimit Property of The [Queryable] attribute has been added back to the OData package.

    See article here for a run down of some of the changes.

    Here is the updated Nuget package. As of this writing it is a PREVIEW package.

    0 讨论(0)
  • 2021-01-12 17:41

    There's an action filter attribute called ResultLimitAttribute which you can use on any action method which returns IQueryable<T> or even IEnumerable<T> to limit the amount of data returned.

    [ResultLimit(100)]
    public IQueryable<Product> Get() {
        // ...
    }
    
    0 讨论(0)
  • 2021-01-12 17:42

    As Cody Clark pointed out, this area has seen quite a number of changes over time. As of the 5.2 version of WebAPI, you now use the EnableQueryAnnotation and use the PageSize parameter rather than QueryableAttribute or ResultLimit. ([Queryable] will still work, but it is marked as obsolete.) Currently you would use the following syntax:

    [EnableQuery(PageSize = 20, MaxTop = 20)]
    public IQueryable<Product> Get() {
        // ...
    }
    

    By using PageSize, you set the default page size for unparameterized requests. If you don't include the MaxTop value, a rogue client could set the top to a very high value and bypass the page defaults. With MaxTop, you will throw an exception if the client requests more records than your API supports.

    0 讨论(0)
  • 2021-01-12 17:58

    There is a short webcast about paging and querying with MVC Web API that you can watch here

    It explains how you can perform paging by either

    • using the OData query syntax, in which case the Web API automatically interprets and retrieves the results for you,
    • or by writing your own method which takes as parameters the pageIndex and pageSize, and then returns the requested number of items.

    AFAIK, you cannot configure the maximum number of items returned by using the first method, but you could limit the number of items returned by using the second approach, and just checking if (pageSize>maxPage) then return maxPage items.

    0 讨论(0)
提交回复
热议问题