6

We have a controller which expects some parameters in a get route, but the OData functions like $top isn't working.

According to the docs it (custom query options) should work fine just declaring the @ prefix at the custom options but it's not:

  • Using @ as the prefix (as suggested at docs) the parameter filtro isn't being filled and get default values to all its properties.
  • Using no prefix it's not returning an error but the $top function is being ignored and I'm getting too many records to show (2K+).

There is another answer here on SO to something similar, but we are using OData V3 which has no explicit Edm Model Builders, it's inferred.

Have you guys solved such an issue?

Here is my code:

GET Request:

~/ProdutosRelevantes?$top=5&
    filtro.Cnpjs[0]=00000000000001&
    filtro.DataInicio=2018-01-01&
    filtro.DataFim=2018-12-01&
    filtro.IndMercado=2&

Controller method:

[HttpGet]
public IHttpActionResult ProdutosRelevantes([FromUri] ParametrosAnalise filtro)
{
    var retorno = GetService().GetProdutosRelevantes(filtro);
    return Content(HttpStatusCode.OK, retorno);
}
public class ParametrosAnalise
  {
      public Guid IdCliente { get; set; }
      public string[] Cnpjs { get; set; }
      public DateTime? DataInicio { get; set; }
      public DateTime? DataFim { get; set; }
      public EnumEscopoMercado? IndMercado { get; set; }      
      // Enum declaration
      public enum EnumEscopoMercado
      {
          [Description("INCLUI NACIONAL")]
          InternoEExterno = 1,
          [Description("EXTERIOR")]
          Externo = 2
      }
  }

Thanks.

Diego Rafael Souza
  • 5,241
  • 3
  • 23
  • 62
  • I have been using odata v4 4ever so I forget what is/isnot possible in v3. It seems that you should be able to filter your result using odata's `$filter` though or am I wrong (you can in v4). That is *usually* the preferred way to do this. Also is there any difference if you were to write `return Ok(rtorno);` instead? – Igor Oct 30 '18 at 14:41
  • In this specific case, it's not that simple to "translate" the object to `$filter` function, there are some business rules to check using the `ParametrosAnalise` object. `Ok(retorno)` have the same effect. – Diego Rafael Souza Oct 30 '18 at 15:06

1 Answers1

5

Do you have enabled oData with [EnableQuery] decorator in your action? Or in your HttpConfiguration => config.EnableQuerySupport()?

https://learn.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options

Max
  • 794
  • 3
  • 7