Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit OData $filter by property in Web API

I want to only allow some properties to be used in OData query $filter option.

I see that there an AllowedOrderByProperties parameter to EnableQueryAttribute, but I didn't find another for $filter. Did I just miss it? If not, what would it take to implement it?

like image 736
nitzanms Avatar asked Nov 15 '25 05:11

nitzanms


1 Answers

You can try the following:

once you have the builder, you can list the properties of the entitySet then you can mention if the field is filterable or not.

var entityTypeConfig = builder.EntitySet<SomeType>("SomeType").EntityType;
entityTypeConfig.Property(x => x.SomeField);
entityTypeConfig.Property(x => x.SomeField2).IsNotFilterable().IsNonFilterable();
// not sure what is the difference between them

and in the controller action (the httpGet for example) add

options.Filter.Validate(allowedOptions);

in case a field is not filterable this would throw an exception.

like image 131
TomerC Avatar answered Nov 17 '25 20:11

TomerC