Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get route using OData and custom query options

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.

like image 429
Diego Rafael Souza Avatar asked Oct 30 '18 14:10

Diego Rafael Souza


People also ask

What are OData query options?

A query option is a set of query string parameters applied to a resource that can help control the amount of data being returned for the resource in the URL.

How do you develop query options for an OData service using code based implementation?

Expand Service Implementation, then expand Products and right click on GetEntitySet (Query) and select Go to ABAP Workbench. This will open automatically open the method ZCL_GW_PRODUCT_DPC_EXT->PRODUCTS_GET_ENTITYSET in SE80. Replace the existing coding in the method by the coding below.


1 Answers

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

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

like image 115
Max Avatar answered Sep 26 '22 00:09

Max