Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OData v4.0 How to set MaxExpansionDepth?


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

like image 290
Nhat Duy Avatar asked Sep 19 '16 11:09

Nhat Duy


2 Answers

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

before your method name add it.

like image 132
Chandrima Das Avatar answered Oct 19 '22 03:10

Chandrima Das


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){ }
like image 24
Gabitu Avatar answered Oct 19 '22 03:10

Gabitu