ASP.NET MVC 4 (web api) OData Configuration

Played with the (Single Page App) BigShelf . I'm very interested in the GetBooksForSearch method (/ api / BigShelf / GetBooksForSearch), which requires additional parameters $filter , $inlinecount , $top , $skip for search results and filtering that are not in the controller code

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

I can’t find any documents about how this controller translates and filters the result afterwards and, more importantly, how to configure this behavior (for example, limit the maximum result), who has the key?

- Updated -

Learned that the MVC Web API does the trick. But how can we customize it?

+6
source share
4 answers

There is a ResultLimitAttribute action filter attribute ResultLimitAttribute , which you can use for any action method that returns IQueryable<T> or even IEnumerable<T> to limit the amount of data returned.

 [ResultLimit(100)] public IQueryable<Product> Get() { // ... } 
+11
source

It looks like ResultLimitAttribute has been deleted. See commit

It has been included in the [Queryable] attribute function, which is now required to support OData. See discussion here

Proper use will now be

[Queryable(ResultLimit = 10)]

[UPDATE]

As of RTM, the ResultLimit function for Queryable has been removed. In addition, [Queryable] has been moved to its own preview package. See this blog for more information and for instructions on new usage.

[UPDATE 2 11-16-12] With the update of the ASP.Net update in the fall of 2012, everything was updated. The ResultLimit property of the [Queryable] attribute has been added back to the OData package.

See the article here for a run of some changes.

Here is the updated Nuget package. This is currently the PREVIEW package.

+14
source

There is a short webcast about swap and request using the MVC Web API, in which you can watch here

It explains how to pager with

  • using the OData query syntax, in which case the web API will automatically interpret and get the results for you,
  • or by writing your own method, which takes pageIndex and pageSize as parameters, and then returns the requested number of elements.

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

+3
source

As noted by Cody Clark, over time, quite a lot of changes have occurred in this area. Starting with WebAPI version 5.2, you now use EnableQueryAnnotation and use the PageSize parameter rather than QueryableAttribute or ResultLimit . ( [Queryable] will still work, but it is marked as deprecated.) You are currently using the following syntax:

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

Using PageSize , you set the default page size for non-parameterized queries. If you did not specify a MaxTop value, a MaxTop client can set the top to a very high value and bypass the default values. With MaxTop you will throw an exception if the client requests more records than your API supports.

+1
source

Source: https://habr.com/ru/post/908954/


All Articles