-
Notifications
You must be signed in to change notification settings - Fork 2
Home
Welcome to the Net.Http.WebApi.OData wiki!
Net.Http.WebApi.OData is a C# library which parses an OData query uri into an object model which can be used to query custom data sources which are not IQueryable
. It was extracted from the MicroLite.Extensions.WebApi library into a separate project so that it could be easily used by other projects.
To use it in your own Web API, firstly install the nuget package Install-Package Net.Http.WebApi.OData
and then in your controller, define a Get method which accepts a single parameter of ODataQueryOptions
:
public IEnumerable<MyClass> Get(ODataQueryOptions queryOptions)
{
// Implement query logic.
}
You can use the ODataValidationSettings
and the Validate extension method to validate the parameters:
public IEnumerable<MyClass> Get(ODataQueryOptions queryOptions)
{
var validationSettings = new ODataValidationSettings
{
AllowedArithmeticOperators = AllowedArithmeticOperators.All,
AllowedFunctions = AllowedFunctions.AllFunctions,
AllowedLogicalOperators = AllowedLogicalOperators.All,
AllowedQueryOptions = AllowedQueryOptions.Filter
| AllowedQueryOptions.Format
| AllowedQueryOptions.InlineCount
| AllowedQueryOptions.OrderBy
| AllowedQueryOptions.Select
| AllowedQueryOptions.Skip
| AllowedQueryOptions.Top,
MaxTop = 50
};
queryOptions.Validate(validationSettings);
// Implement query logic.
}
Validate will throw an ODataException
if the ODataQueryOptions
contain any values which the service does not allow based upon the specified validation settings.
The ODataQueryOptions
contains properties which can be interrogated to understand the specified query.
Consider the following OData query:
http://localhost/api/Customers?$select=FirstName,LastName,DateOfBirth&$orderby=LastName desc,FirstName
The ODataQueryOptions.Select
would be as follows:
queryOptions.Select.RawValue; // "$select=FirstName,LastName,DateOfBirth"
queryOptions.Select.Properties.Count; // 3
queryOptions.Select.Properties[0]; // "FirstName"
queryOptions.Select.Properties[1]; // "LastName"
queryOptions.Select.Properties[2]; // "DateOfBirth"
The ODataQueryOptions.OrderBy
would be as follows:
queryOptions.OrderBy.RawValue; // "$orderby=LastName desc,FirstName"
queryOptions.OrderBy.Properties.Count; // 2
queryOptions.OrderBy.Properties[0].Direction; // OrderByDirection.Descending
queryOptions.OrderBy.Properties[0].Name; // LastName
queryOptions.OrderBy.Properties[0].RawValue; // "LastName desc"
queryOptions.OrderBy.Properties[1].Direction; // OrderByDirection.Ascending
queryOptions.OrderBy.Properties[1].Name; // FirstName
queryOptions.OrderBy.Properties[1].RawValue; // "FirstName"
Net.Http.WebApi.OData is currently built to support OData 3.0, the OData Query Options specify the following query options and Net.Http.WebApi.OData currently supports them if ticked:
Operator | Net.Http.WebApi.OData 3.4.x | Net.Http.WebApi.OData 4.x |
---|---|---|
eq | implemented | implemented |
ne | implemented | implemented |
gt | implemented | implemented |
ge | implemented | implemented |
lt | implemented | implemented |
le | implemented | implemented |
and | implemented | implemented |
or | implemented | implemented |
not | implemented | implemented |
Operator | Net.Http.WebApi.OData 3.4.x | Net.Http.WebApi.OData 4.x |
---|---|---|
add | implemented | implemented |
sub | implemented | implemented |
mul | implemented | implemented |
div | implemented | implemented |
mod | implemented | implemented |
Operator | Net.Http.WebApi.OData 3.4.x | Net.Http.WebApi.OData 4.x |
---|---|---|
() Precedence Grouping |
implemented | implemented |
Operator | Net.Http.WebApi.OData 3.4.x | Net.Http.WebApi.OData 4.x |
---|---|---|
substringof | implemented | not in spec (use contains ) |
endswith | implemented | implemented |
startswith | implemented | implemented |
length | implemented | implemented |
indexof | implemented | implemented |
replace | implemented | implemented |
substring | implemented | implemented |
tolower | implemented | implemented |
toupper | implemented | implemented |
trim | implemented | implemented |
concat | implemented | implemented |
contains | not in spec (use substringof ) |
implemented |
Operator | Net.Http.WebApi.OData 3.4.x | Net.Http.WebApi.OData 4.x |
---|---|---|
day | implemented | implemented |
hour | implemented | implemented |
minute | implemented | implemented |
month | implemented | implemented |
second | implemented | implemented |
year | implemented | implemented |
now | not in spec | implemented |
mindatetime | not in spec | implemented |
maxdatetime | not in spec | implemented |
fractionalseconds | not in spec | implemented |
Operator | Net.Http.WebApi.OData 3.4.x | Net.Http.WebApi.OData 4.x |
---|---|---|
round | implemented | implemented |
floor | implemented | implemented |
ceiling | implemented | implemented |
Operator | Net.Http.WebApi.OData 3.4.x | Net.Http.WebApi.OData 4.x |
---|---|---|
IsOf | implemented | implemented |
Cast | implemented | implemented |
Operator | Net.Http.WebApi.OData 3.4.x | Net.Http.WebApi.OData 4.x |
---|---|---|
$expand=Orders |
implemented | implemented |
Operator | Net.Http.WebApi.OData 3.4.x | Net.Http.WebApi.OData 4.x |
---|---|---|
wildcard $select=*
|
implemented | implemented |
properties $select=Name, DateOfBirth
|
implemented | implemented |
nested properties $select=Address.Postcode
|
not implemented | not implemented |
Operator | Net.Http.WebApi.OData 3.4.x | Net.Http.WebApi.OData 4.x |
---|---|---|
asc $orderby=Name asc
|
implemented | implemented |
desc $orderby=Name desc
|
implemented | implemented |
Operator | Net.Http.WebApi.OData 3.4.x | Net.Http.WebApi.OData 4.x |
---|---|---|
$top=5 |
implemented | implemented |
Operator | Net.Http.WebApi.OData 3.4.x | Net.Http.WebApi.OData 4.x |
---|---|---|
$skip=10 |
implemented | implemented |
Operator | Net.Http.WebApi.OData 3.4.x | Net.Http.WebApi.OData 4.x |
---|---|---|
$count=true |
not in spec (use $inlinecount=allpages ) |
implemented |
$count=false |
not in spec (use $inlinecount=none ) |
implemented |
Operator | Net.Http.WebApi.OData 3.4.x | Net.Http.WebApi.OData 4.x |
---|---|---|
$inlinecount=allpages |
implemented | not in spec (replaced by $count=true ) |
$inlinecount=none |
implemented | not in spec (replaced by $count=false ) |
Operator | Net.Http.WebApi.OData 3.4.x | Net.Http.WebApi.OData 4.x |
---|---|---|
$format=atom |
implemented | implemented |
$format=json |
implemented | implemented |
$format=xml |
implemented | implemented |
$format=... where ... is application/?
|
implemented | implemented |
URI Path | Net.Http.WebApi.OData 3.4.x | Net.Http.WebApi.OData 4.x |
---|---|---|
/$links/ |
not implemented | not implemented |
URI Path | Net.Http.WebApi.OData 3.4.x | Net.Http.WebApi.OData 4.x |
---|---|---|
/$count |
not implemented | not implemented |