Skip to content
This repository has been archived by the owner on Dec 29, 2020. It is now read-only.
Trevor Pilley edited this page Jun 23, 2017 · 55 revisions

Welcome to the Net.Http.WebApi.OData wiki!

NuGet version

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.

To use it in your own Web API, firstly install the nuget package:

PM> Install-Package Net.Http.WebApi.OData

Configuration

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Configure routes, etc

        // Build the OData Entity Data Model
        var entityDataModelBuilder = new EntityDataModelBuilder();
        entityDataModelBuilder.RegisterEntitySet<Category>("Categories", x => x.Name);
        entityDataModelBuilder.RegisterEntitySet<Product>("Products", x => x.Name);
        entityDataModelBuilder.BuildModel();
    }
}

Note that when you register an Entity Set, you also specify the name of the Entity Set. The name needs to match the URL you intend to use so if you use http://myservice/api/Products then register the Entity Set using .RegisterEntitySet<Product>("Products");, if you use http://myservice/api/Product then register the Entity Set using .RegisterEntitySet<Product>("Product");.

Getting the OData Query Options

In your controller, define a Get method which accepts a single parameter of ODataQueryOptions:

public IEnumerable<MyClass> Get(ODataQueryOptions queryOptions)
{
    var validationSettings = new ODataValidationSettings { ... };
    queryOptions.Validate(validationSettings);

    // Implement query logic.
}

The ODataQueryOptions class contains properties which can be interrogated to understand the specified query.

Next: Validating OData Query Options

Clone this wiki locally