Search code examples
odataasp.net-web-api2

How to return navigation property with an OData WebAPI Action


I was wondering how you would return a navigation property with an ODataWebApi Action.

Example: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-routing-conventions

In the link above, they mention the example:

POST /entityset(key)/cast/action /Products(1)/Models.Book/CheckOut

Where Checkout is the action. Say that this URL returns a Book object that has a navigation property.

Imagine the Book structure below:

Book - string Name - List Pages

Pages - string Name

Pages would be marked considered a navigation property.

In my case, whenever I trigger the Post call on the API I only get

Book - string Name

And Pages is left out.

The example below is a modified example of what was mentioned about. Basically an api that get a checks out a collection of books.

When i hit the URL: http://localhost:4448/api/v1/Books/Action.Checkout?$format=application/json;odata.metadata=full

I get the following response. It's missing the actual pages.....and just have navigation reference to pages.

   {
        @odata.context: "HTTP://LOCALHOST:4448/API/V1/$METADATA#BOOK/$ENTITY"
        @odata.type: "#Book"
        @odata.id: "HTTP://LOCALHOST:4448/API/V1/BOOK(0)"
        @odata.editLink: "HTTP://LOCALHOST:4448/API/V1/BOOK(0)"
        [email protected]: "#Int64"
        ID: 0
        Name: null
        [email protected]: "#BookTypeEnum"
        Type: "Book"
        [email protected]: "#DateTimeOffset"
        CreationDate: "0001-01-01T00:00:00-08:00"
        [email protected]: "#DateTimeOffset"
        ModifyDate: "0001-01-01T00:00:00-08:00"
        Owner: null
        [email protected]: "HTTP://LOCALHOST:4448/API/V1/BOOK(0)/PAGES/$REF"
        [email protected]: "HTTP://LOCALHOST:4448/API/V1/BOOK(0)/PAGES"
    }

Any advice appreciated, Thanks D


Solution

  • The solution is to enable the query for OData. Specify [EnableQuery] for the function in the controller. This will give you the ability to http://localhost:4448/api/v1/Books/Action.Checkout?$expand=Pages