22

I am using OData Web API for Version 4, when I try to query OData web Api using $top parameter, it return me following exception message.

The query specified in the URI is not valid. The limit of '0' for Top query has been exceeded. The value from the incoming request is '10'

I am using Apache Ignite dotNet LINQ as data source instead of Entity Framework, my OData controller action method is as follows:

[EnableQuery]
public IQueryable<Productioncurvepnl> GetProductioncurvepnl()
{
    Console.WriteLine("Starting query to ignite");
    var q = AIgniteClient.IgniteClient.Instance.ProductionCurvePnLCache.AsCacheQueryable().Select(c => c.Value);
    return q;
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Abdul Qadir Memon
  • 950
  • 1
  • 12
  • 27

5 Answers5

68

Since Web API OData V6.0.0 you need to enable query options to have this work. This can be done globally in the WebApiConfig.Register(HttpConfiguration config)

config.Select().Expand().Filter().OrderBy().MaxTop(null).Count();

or directly on your models, for fine grained configuration:

[Page(MaxTop = 100)]
public class Products

If you're using Model Bound Fluent APIs and cannot add attributes, you'll need to append the query options. For example .Page(50, 50):

 builder.EntitySet<AccountRecordDto>("Accounts").EntityType.Expand(1, 
 "Transactions").Count().Page(50, 50);

More details can be found in the documentation

ogrim
  • 968
  • 9
  • 17
  • 3
    for `config.Select()` to work add `using System.Web.OData.Extensions;` hope helps someone. – Shaiju T Aug 10 '17 at 08:24
  • Annotating the class worked for me, but the way you enabled query options did not. I used config.AddODataQueryFilter(new EnableQueryAttribute { AllowedQueryOptions = AllowedQueryOptions.All }); – Gojira Aug 27 '21 at 18:31
  • (N.B. I am using HttpSelfHostConfiguration which is probably why) – Gojira Aug 27 '21 at 18:41
5

Adding below in Startup.cs works for me

config.Select().Expand().Filter().OrderBy().MaxTop(null).Count();
jt03
  • 131
  • 2
  • 3
1

Based on the returned error message the problem most likely is that the MaxTop is not defined. You can do that using the EnableQueryAttribute on your method like so (change the value as you see fit), I used a value of 100.

[EnableQuery(MaxTop = 100)]
public IQueryable<Productioncurvepnl> GetProductioncurvepnl()
{
    Console.WriteLine("Starting query to ignite");
    var q = AIgniteClient.IgniteClient.Instance.ProductionCurvePnLCache.AsCacheQueryable().Select(c => c.Value);
    return q;
}

See EnableQueryAttribute for more details.

Igor
  • 60,821
  • 10
  • 100
  • 175
1

For me [EnableQuery (Max Top = 100)] not working, but [Queryable] working fine. [EnableQuery (Max Top = 100)] should work but don't know why it's not working. Someone knows then please let me know. But for now I am using [Queryable].

Uddhao Pachrne
  • 531
  • 1
  • 8
  • 22
  • 2
    Queryable is deprecated, I have posted an answer with two approaches. Either configuring it globally, or per model – ogrim Oct 13 '16 at 12:36
  • Neither of the two methods work if you've applied restrictions to the EntityType, for example: `builder.EntitySet("Accounts").EntityType.Expand(1, "Transactions").Count();` In this case, I needed to add .Page(50,50) to the EntityType. – Liam Fleming May 29 '18 at 04:31
  • @ogrim neither work for me with asp core which there is no `WebApiConfig.Register(HttpConfiguration config)` – Glass Cannon Oct 01 '18 at 11:46
0

I had the same problem and I resolved it by set setting 'SetMaxTop' property in my .Net 6 Odata Middleware :

builder
   .Services
   .AddControllers(mvcoptions => mvcoptions.EnableEndpointRouting = false)
   .AddOData(options => options.Select().Filter().Count().OrderBy().Expand().SetMaxTop(1000));
Mahyar Mottaghi Zadeh
  • 1,178
  • 6
  • 18
  • 31
MUTIJIMA
  • 21
  • 4