Search code examples
c#odataodata-v4

How can I make a nested OData call in .NET using OData v4 6.0.0?


Assuming I have the following in my WebApiConfig.cs:

        modelBuilder.EntitySet<Content>("Content");
        modelBuilder.EntitySet<Area>("Area");

And I have the following classes:

public class Area
{
    public string Id { get; set; }

    public ICollection<Content> Contents { get; set; }
}

public class Content
{
    public string Id { get; set; }

    [ForeignKey("Area")]
    public int? AreaId { get; set; }
    public virtual Area Area { get; set; }
}

How can I make an ODataController action method that is bound to the following route?

GET /odata/Area(Id)/Content(Id)

When I try to make a custom routing convention, I keep getting the following ODataPath:

"~/entityset/key/unresolved"

and I'm trying to get this:

"~/entityset/key/navigation/key"

Note: I am using v6.0.0 of the v4 OData, so some of the routing has changed from previous versions.


Solution

  • The name Content in the URL /odata/Area(Id)/Content(Id) needs to match the name of the navigation property on the class Area. On your class in the question, it is called Contents