1
votes

I'm Using ASP.NET Web API OData with EntitySetController,

Trying to Create Link between two entities: ProductFamily and Product,

OData Action:

public override void CreateLink([FromODataUri] int key, string navigationProperty, [FromBody] Uri link)

ajax request:

$.ajax({
    url: "api/ProductFamily(1)/$links/Products",
    type: "PUT",
    //data missing for the "[FromBody] Uri link"?? what is the format of the data??
    success: function (result) {
        //do something
    }
});

after sending the ajax request, it successfully running into the CreateLink Action, but the link parameter is always null,

what kind of data is missing in the ajax request?? what is the format??

Is there any Sample??

Edit 1: Should be like this??

ajax request:

$.ajax({
    url: "api/ProductFamily(1)/$links/Products",
    type: "PUT",
    data: {"url":"api/Products(5)"}
    success: function (result) {
        //do something
    }
});

this will cause a System.InvalidOperationException: "No MediaTypeFormatter is available to read an object of type 'Uri' from content with media type 'application/json'."

UPDATE:

Problem Solved. Steps:

  1. Using Nightly ASP.NET Web Stack
  2. Getting started with ASP.NET Web API OData in 3 simple steps
1
OMG! TY! I've been trying for days to get /odata/Entity(1)/CollectionProp/$ref to work but changing the method on the odata controller from CreateRef to CreateLink and using /odata/Entity(1)/$links/CollectionProp now works!! - Paul - Soura Tech LLC

1 Answers

2
votes

The request body should look like this:

{"url": "http://www.microsoft.com/"}

with an application/json content-type. If that doesn't work, try updating to our latest build.