Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 655
xandy Avatar asked Feb 21 '12 08:02

xandy


2 Answers

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.

like image 58
Cody Clark Avatar answered Oct 21 '22 01:10

Cody Clark


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() {
    // ...
}
like image 23
marcind Avatar answered Oct 21 '22 00:10

marcind