Search code examples
odataasp.net-web-api

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?


Solution

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