I have the following complex type to return from an Web Api ODATA controller:
public class TreeItem
{
public int Id { get; set; }
public string Name { get; set; }
public List<TreeItem> Children { get; set; }
}
This is the controller method:
// GET: odata/TreeItems
public IHttpActionResult GetTreeItems(ODataQueryOptions<TreeItem> queryOptions)
{
// validate the query.
try
{
queryOptions.Validate(_validationSettings);
}
catch (ODataException ex)
{
return BadRequest(ex.Message);
}
var treeItems = new List<TreeItem>();
treeItems.Add(new TreeItem() { Id = 1, Name = "Geleiding", Children = new List<TreeItem>() { new TreeItem(){ Id=4, Name="Item1-1", Children=new List<TreeItem>()} } });
treeItems.Add(new TreeItem() { Id = 2, Name = "Item 2", Children = new List<TreeItem>() });
treeItems.Add(new TreeItem() { Id = 3, Name = "Item 3", Children = new List<TreeItem>() });
return Ok<IEnumerable<TreeItem>>(treeItems);
}
In WebApiCOnfig: builder.EntitySet("TreeItems").EntityType.ComplexProperty(c => c.Children);
When I go to this URL: http://localhost:50997/odata/TreeItems
I get this response:
{
"odata.metadata":"http://localhost:50997/odata/$metadata#TreeItems","value":[
{
"Children":{
"Capacity":4
},"Id":1,"Name":"Geleiding"
},{
"Children":{
"Capacity":0
},"Id":2,"Name":"Item 2"
},{
"Children":{
"Capacity":0
},"Id":3,"Name":"Item 3"
}
]
}
How do I get the Children list to show completely including all levels it may have?
$expand=Children does not work, says Children is not a navigational property.
Thanks for you help!