0

我试图找到一个如何正确实例化 ODataNavigationLink 的示例,以防它为非空。我发现的唯一代码示例创建了一个非扩展链接,但没有将其绑定到任何数据:

//create a non-expanded link for the orders navigation property
  writer.WriteStart(new ODataNavigationLink()
  {
      IsCollection = true,
      Name = "Orders",
      Url = new Uri("http://microsoft.com/Customer(" + 
                 dataSource.Customers.First().CustomerID + ")/Orders")
  });
  writer.WriteEnd(); //ends the orders link

所以在这里我们指定“订单”的链接。但是我如何提供链接的实际值(在此示例中,链接是一个集合,但它也可以是单个条目)。当我手动编写有效负载时,我提供了带有链接条目 ID 的“href”属性。我无法弄清楚这是如何使用 ODataLib 完成的。

4

1 回答 1

1

The value appear in the 'href' attribute just shows the Url property of ODataNavigationLink, so you can try the following code to set it manually:

//create a non-expanded link for the orders navigation property
writer.WriteStart(new ODataNavigationLink() { 
    IsCollection = true,
    Name = "Orders",
    Url = new Uri("http://microsoft.com/Orders(3)") }); 
writer.WriteEnd(); //ends the orders link

In common, the navigation link should be the source entity url followed by the navigation property,see here, while id should point to the real entry ID.

updated:

According to the latest feedback, you're trying to write the 'Collection of links' as described in section 14.1 of the atom spec. Thus you can try ODataEntityReferenceLinks class:

var referenceLink1 = new ODataEntityReferenceLink { Url = new Uri("http://host/Orders(1)") };
var referenceLink2 = new ODataEntityReferenceLink { Url = new Uri("http://host/Orders(2)") };
var referenceLink3 = new ODataEntityReferenceLink { Url = new Uri("http://host/Orders(3)") };
var referenceLinks = new ODataEntityReferenceLinks
{
    Links = new[] { referenceLink1, referenceLink2, referenceLink3 }
};
writer.WriteEntityReferenceLinks(referenceLinks);

and the payload would be something like:

<links xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices">
  <uri>http://host/Orders(1)</uri>
  <uri>http://host/Orders(2)</uri>
  <uri>http://host/Orders(3)</uri>
</links>
于 2014-09-02T06:33:21.113 回答