5

Hi Everyone,

  • I need a setting in Get() method [EnableQuery(MaxExpansionDepth=3)] to limit expansion depth in OData query.

  • I tried to set this attribute in Get() method but it does not work.

Could you please give me a suggestion for this?

Follow as : https://github.com/OData/odata.net

Many Thanks

Nhat Duy
  • 198
  • 1
  • 2
  • 11
  • Hi, please show us the actual code you have tried and the exact error message. "Does not work" is not sufficient for us to understand the problem. – Stefan Sep 19 '16 at 11:03
  • I want to have an error message when I expand level more than Max Expansion Depth. E.g. "var queryOptions = new ODataQueryOptions(queryContext, Request)" --> queryOptions.SelectExpand.SelectExpandClause should show an exception "Not allowed - the level expansion is more than Max expansion depth" I don't know where to set Max Expansion Depth. Please give me your advice, Stefan. Thanks – Nhat Duy Sep 19 '16 at 11:18
  • Which library you are using? And which version you are using? If you are using Web Api OData 6.0, you can refer to http://odata.github.io/WebApi/#13-01-modelbound-attribute, and see Expand attribute. – Vincent Sep 20 '16 at 00:47
  • I'm using Web Api OData 6.0, currently, I use below method to handle the validations: "enableQueryAttribute.ValidateQuery(Request, queryOptions)" Thanks all. – Nhat Duy Sep 20 '16 at 08:18

2 Answers2

9
[EnableQuery(MaxExpansionDepth = 4)]
public IQueryable<abc> Get() 
{
    return GetAQueryable<abc>();
}

before your method name add it.

War
  • 8,539
  • 4
  • 46
  • 98
Chandrima Das
  • 136
  • 1
  • 2
  • 5
2

In my case, I needed to set the maximum expansion depth in the entity when creating the edm model.

In the startup, you configure your route and your model:

routes.MapODataServiceRoute("odata", "odata", ODataDataSourceProvider.GetEdmModel(new ODataConventionModelBuilder()));

In your provider:

public IEdmModel GetEdmModel(ODataModelBuilder builder)
    {
        builder.EntitySet<Object>("Objects");

        builder
            .EntityType<Object>()
            .Filter() // Enables filtering
            .Expand(3) // Enables expanding with maximum depth: 3
            .Select(); // Enables selecting

        return builder.Build();
    }

Then, in your controller you can override the maximum depth value, as long as it is less than the maximum defined in the entity configuration:

[HttpGet]
[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All, MaxExpansionDepth = 2)]
public SingleResult<Object> Get(Guid key){ }
War
  • 8,539
  • 4
  • 46
  • 98
Gabitu
  • 179
  • 5