访问OData属性 [英] Accessing OData Property

查看:92
本文介绍了访问OData属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对oData Web服务非常陌生.我想在移动平台上获取并填充以下输出.我可以在移动平台 http://services.odata中填充以下网址数据. org/V4/Northwind/Northwind.svc/Customers .但是,一旦我开始做另一项运动.我被下面的odata输出卡住了.如何访问属性,例如名称或描述?

I am quite new to oData webservices. I would like to get and populate the following output on mobile platform. I could able populate the following url data on mobile platform http://services.odata.org/V4/Northwind/Northwind.svc/Customers . However, once I started on doing another exercise. I am stuck with the following odata output. How could I access to properties, such as Name or Description?

PUT /OData/OData.svc/Products(1) HTTP/1.1 Host: services.odata.org DataServiceVersion:
  1.0 MaxDataServiceVersion: 2.0 accept: application/atom+xml 
  content-type: application/atom+xml Content-Length: 1181 
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Entry xml:base="http://services.odata.org/OData/OData.svc/" 
    xmlns:d=" http://schemas.microsoft.com/ado/2007/08/dataservices"
    xmlns:m=" http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" 
    xmlns="http://www.w3.org/2005/Atom"> 
  <id>http://services.odata.org/OData/OData.svc/Products(1)</id>
  <title type="text"></title> 
  <updated>2010-02-28T10:23:02Z</updated>
  <author> 
    <name /> 
  </author> 
  <Link rel="edit" title="Product" href="Products(1)" /> 
  <category term="DataServiceProviderDemo.Product" 
      scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> 
  <content type="application/xml"> 
    <m:properties> 
      <d:ID m:type="Edm.Int32">1</d:ID>
      <d:Name>Milk</d:Name> 
      <d:Description>Low fat milk</d:Description>
      <d:ReleaseDate m:type="Edm.DateTime">1995-10-21T00:00:00</d:ReleaseDate>
      <d:DiscontinuedDate m:type="Edm.DateTime" m:null="true" /> 
      <d:Rating m:type="Edm.Int32">4</d:Rating>
      <d:Price m:type="Edm.Decimal">4.5</d:Price> 
    </m:properties> 
  </content>
</Entry>

推荐答案

有几种访问特定属性的方法,因为实体上有两种属性:非导航属性和导航属性.

There are several ways to access specific properties, as there are two kinds of properties on a entity: a non-navigation property and a navigation property.

非导航属性可以是原始类型属性,原始类型属性的集合,复杂类型属性,复杂类型属性或流属性.查询实体集或特定实体时,默认情况下,非导航属性的值是实体有效载荷的内联:

A non-navigation property is either a primitive type property, a collection of primitive type property, a complex type property, a complex type property, or a stream property. When you query the entity set or a specific entity, the values of the non-navigation properties are by default inline of the entity payload:

例如查询时,IDNameDescription等是内联的:

e.g. ID, Name, Description, etc. are inline when you query:

GET http://services.odata.org/v4/odata/odata.svc/Products

如果要选择所需的属性,可以使用$select查询选项.例如

If you want to choose the properties you need, you can use the $select query option. E.g.

GET http://services.odata.org/v4/odata/odata.svc/Products?$select=ID,Name

通过添加此类查询选项,您将仅找到需要在有效负载内联的属性.

By appending such query option, you will find only the properties you need inline of the payload.

如果您只想访问属性值,则应将属性名称作为段附加到单个实体的请求URL中.例如

If you want to only access the property value, you should append the property name as a segment to the request URL to a single entity. E.g.

GET http://services.odata.org/v4/odata/odata.svc/Products(1)/ID

还有另一种属性:导航属性.它们是实体类型属性或实体类型属性的集合.导航属性描述了服务中不同实体之间的关系.一个示例是Product实体上的Categories导航属性.

There is another kind of property: the navigation properties. They are either an entity type property or a collection of entity type property. Navigation properties describes the relationship between the different entities in the service. An example is the Categories navigation property on the Product entity.

默认情况下,导航属性未在实体有效负载的内联中显示.为了将它们包括在内,需要使用$expand查询选项:

Navigation properties are by default not shown inline of the entity payload. In order to include them inline, the $expand query option needs to be used:

GET http://services.odata.org/v4/odata/odata.svc/Products?$expand=Categories

如果您只想访问导航属性,则请求URL与非导航属性的请求URL相似:

If you want to only access the navigation property, the request URL is similar as it is for non-navigation properties:

GET http://services.odata.org/v4/odata/odata.svc/Products(1)/Categories

要了解有关如何针对不同情况发出不同的OData请求以及URL约定是什么的更多信息,以下材料会有所帮助:

To learn more about how to issue different OData requests for different scenarios and what the URL conventions is, the following materials are helpful:

关于OData.org的教程: http://www.odata.org/Getting Started/Basic-tutorial/(基本), http://www.odata.org/getting-started/advanced-tutorial/(高级).

The tutorials on OData.org: http://www.odata.org/getting-started/basic-tutorial/ (basic), http://www.odata.org/getting-started/advanced-tutorial/ (advanced).

OData V4的URL约定规范:

The URL convention spec of OData V4: http://docs.oasis-open.org/odata/odata/v4.0/os/part2-url-conventions/odata-v4.0-os-part2-url-conventions.html

OData V4的协议规范:

The protocol spec of OData V4: http://docs.oasis-open.org/odata/odata/v4.0/os/part1-protocol/odata-v4.0-os-part1-protocol.html

这篇关于访问OData属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆