xandy
xandy

Reputation: 27421

ASP.net MVC 4 (web api) OData Configuration

Been playing around with the (Single Page App) BigShelf sample. I found really interesting is the GetBooksForSearch method (/api/BigShelf/GetBooksForSearch) that it takes additional $filter, $inlinecount, $top, $skip parameters for paging and filtering results, which are not present in the controller code:

public IQueryable<Book> GetBooksForSearch
    (string profileIds, Sort sort, bool sortAscending)

I coudln't find any documents about how that Controller translate and filter the result afterwards and more importantly, how to configure such behavior (e.g., limit the max result), anyone have a clue?

-- Updated --

Found out that MVC Web API is doing the trick. But how can we configure it?

Upvotes: 6

Views: 6784

Answers (4)

Jim Wooley
Jim Wooley

Reputation: 10418

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.

Upvotes: 1

Cody Clark
Cody Clark

Reputation: 141

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.

Upvotes: 14

marcind
marcind

Reputation: 53183

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() {
    // ...
}

Upvotes: 11

Szilard Muzsi
Szilard Muzsi

Reputation: 1901

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.

Upvotes: 3

Related Questions