Take the 2-minute tour ×

I have a simple data model consisting of two entities

public class product 
     public int ID {get;set;}
     public string Name {get;set;}

public class supplier
    public int ID {get;set;}
    public string Name {get;set;}
    public IEnumerable<product> products {get;set;}

Now from my WebApi odata controller I want to return the supplier with all their products. But I cannot seem to get this working, with it just returning the suppler and effectively stripping the product information. The controller method is a simple Get and GetEntityByKey. My configuration is as follows.

 ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();

Is there a configuration options I'm missing to get this to work?

share|improve this question

2 Answers 2

You didn't mention the URI you're using to get the supplier entity, but I'm going to assume it looks something like: http://.../ServiceRoot.svc/supplier(1). By default in OData, navigation properties are not expanded; that is, by default, requesting a supplier will not include the IDs and Names of the linked products unless you ask for them explicitly via the $expand query option. For example: http://.../ServiceRoot.svc/supplier(1)?$expand=products.

If you don't expand the navigation property, then the products property of the supplier will show up as just a collection of links to the product entities. If you aren't seeing links to the products in the response payload, it could be because you're using the new v3 JSON format of OData, which may omit navigation links that follow the general OData URI conventions (since the client can generate those links itself).

If you include the request URI and payload you're getting back, I can be a bit clearer on what's happening in your situation.

A quick note on terminology: "complex type" in OData usually refers to a structural type that has no identity. A typical example of this could be an Address type, which is a value type that has multiple components (city, country, street, etc.) but doesn't need to have its own key. What you're talking about here are navigations between entities.

share|improve this answer
To build on Jen's answer, $expand isn't supported in the first release of Web API OData on NuGet, but it is currently available in our nightly builds and should be available in the next release. See this blog post for more information on how to get access to our nightlies: blogs.msdn.com/b/henrikn/archive/2012/06/01/… –  Youssef Moussaoui May 31 '13 at 1:07
Thanks for getting back to me. At least I know its not a configuration option and its odata itself stripping out the additional 'product' information, rather than including it in the returned Json as it would in a non odata WebApi Get. Is there a release date for the next version? –  James Baldwinson May 31 '13 at 10:17
@YoussefMoussaoui I have obtained the nightly build, but cannot get a simple example to work, note I am using fiddler composer mywebsite:port/odata/suppler(1)?$expand=products. The code in the webapi controller is as follows protected override supplier GetEntityByKey(int key) { supplier s = new supplier() { Name = "Test", ID=1 }; s.products = new List<product>(){new product() { ID = 1, Name = "P1" }, new product() { Name = "P2", ID = 2 }}; return s; } –  James Baldwinson May 31 '13 at 14:27
Can you elaborate? How isn't it working? –  Youssef Moussaoui May 31 '13 at 17:50
@YoussefMoussaoui - Well the returned JSON does not contain the product information, only the supplier information. Debugging the controller GetEntityKey I can see that the returned value contains the products, but when looking at the Json in Fiddler, the products have still been stripped out. It is working correctly for the Get (returning supplier and products) but not for GetEntityByKey, where it only returns the supplier. Thanks Youssef for your time on this. –  James Baldwinson May 31 '13 at 22:34

Maybe try to use QueryableAttribute. Look here:

Expand support

2) Supporting $select and $expand on single entities through QueryableAttribute.

share|improve this answer

Your Answer


