过帐实体类型带孩子到(MVC的Web API)的OData服务 [英] POSTing an entity type with children to an (MVC Web Api) OData service

查看:163
本文介绍了过帐实体类型带孩子到(MVC的Web API)的OData服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在四处寻找的答案,下面的问题,但至今没有找到。

I've been looking around for an answer to the following questions, but have so far not found one.


  1. 是否OData的标准支持做含子实体对象实体对象一个POST请求?

  2. 如果是这样,那么在ASP.NET MVC网页API的OData框架( EntitySetController )支持这一开箱?

  1. Does the OData standard support doing a POST request containing an entity object with child entity objects?
  2. If so, does the ASP.NET MVC Web Api OData framework (EntitySetController) support this out of the box?

我心目中的情形是这样的东西下面。你有一个对象,包含几个儿童的对象。你能然后做一个POST有这样的身体吗?

The scenario I have in mind goes something like the following. You have a Parent object, containing several Child objects. Could you then do a POST with a body like this?

{ "Property1" : "Property value", "Property2" : 100 
     "Children" : [
          { "ChildProperty" : "Some value" },
          { "ChildProperty" : "Some other value" },
          { "ChildProperty" : "Some third value" }
     ]
}

另外,如果我以后想通过发布一个单独的儿童另一个孩子添加到的对象,是有连接在一起的一个标准化的方式?类似

Also, if I later would like to add another child to the Parent object through POSTing a single child, is there a standardized way of linking them together? Something like

 { "ChildProperty" : "Fourth child property val", "Parent" : 321 }

其中'321'是父对象的ID?

where '321' is the ID of the parent object?

对于任何指针非常感谢!

Many thanks for any pointers!

推荐答案

是,OData的支持这和Web API的OData支持它。 OData的称之为深插入。这里是链接到<一个href=\"http://docs.oasis-open.org/odata/odata/v4.0/cs01/part1-protocol/odata-v4.0-cs01-part1-protocol.html#_Toc365046309\">spec.

Yes, OData supports this and web API OData supports it as well. OData calls it deep insert. Here is the link to the spec.

现在可以有两种类型的深插入,

Now there can be two types of deep inserts,

1)嵌套的项目是新的,以及,应创建。 JSON的有效载荷的一个例子是这样的,

1) The nested item is new as well and should be created. An example of the json payload looks like this,

{
    ‘Property1’: 42,
    ‘Property2’: ‘Contoso’,
    ‘Children’: [
    {
        ‘ChildProperty’: 1,
        ……….
    },
    {
        ‘ChildProperty’: 2,
        ……….
    }]
}

2)嵌套项目已经存在,父项是新的,必须链接到嵌套的项目。的OData协议调用此绑定。样品有效载荷的样子,

2) The nested items already exist, the parent item is new and must be linked to the nested items. OData protocol calls this bind. Sample payload looks like,

{
    ‘Property1’: 42,
    ‘Property2’: ‘Contoso’,
    ‘Children@odata.bind’: [
        "http://localhost/Children(1)",
        "http://localhost/Children(2)",
    ]
}

网页API的OData支持第一种深插入的和不具有所结合的支持(第二个例子)。在第一个有效载荷的情况下,您的控制器将获得与孩子收集正确填充一个对象。

Web API OData supports first kind of deep inserts and doesn't have support for binds (the second example). In case of the first payload, your controller would receive a Parent object with the Children collection properly populated.

和关于你的第二个问题,你有两个选择,

And regarding your second question, you have two options,

1)发表孩子〜/父母(321)/儿童网​​址

1) POST the child to ~/Parents(321)/Children URL

2)POST孩子〜/儿童,然后从这个回应网址〜/父母POST这个孩子的ID链接( 321)/ $链接/儿童

2) POST the child to ~/Children and then POST the ID link of this child from this response to the URL ~/Parents(321)/$links/Children.

您可以参考此<一个href=\"http://docs.oasis-open.org/odata/odata/v4.0/cs01/part1-protocol/odata-v4.0-cs01-part1-protocol.html#_Toc365046313\">link为规范OData的这部分。

You could refer to this link for this part of the OData spec.

这篇关于过帐实体类型带孩子到(MVC的Web API)的OData服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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