将具有子项的实体类型发布到(MVC Web Api)OData服务

时间:2021-01-31 19:34:30

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

我一直在寻找以下问题的答案,但到目前为止还没找到。

  1. Does the OData standard support doing a POST request containing an entity object with child entity objects?
  2. OData标准是否支持执行包含具有子实体对象的实体对象的POST请求?

  3. If so, does the ASP.NET MVC Web Api OData framework (EntitySetController) support this out of the box?
  4. 如果是这样,ASP.NET MVC Web Api OData框架(EntitySetController)是否支持开箱即用?

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?

我想到的场景如下所示。您有一个Parent对象,包含多个Child对象。你可以用这样的身体做一个POST吗?

{ "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

此外,如果我后来想通过POST一个孩子将另一个子项添加到Parent对象,是否有一种标准化的方法将它们链接在一起?就像是

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

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

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

Many thanks for any pointers!

非常感谢任何指针!

1 个解决方案

#1


6  

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

是的,OData支持这一点,Web API OData也支持它。 OData称之为深插入。这是规范的链接。

Now there can be two types of deep inserts,

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

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

1)嵌套项也是新的,应该创建。 json有效负载的示例如下所示,

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

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,

2)嵌套项已经存在,父项是新项,必须链接到嵌套项。 OData协议调用此绑定。示例负载看起来像,

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

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.

Web API OData支持第一种深度插入,并且不支持绑定(第二个示例)。如果是第一个有效负载,您的控制器将收到一个父对象,其中正确填充了Children集合。

And regarding your second question, you have two options,

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

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

1)将孩子张贴到〜/ Parents(321)/ Children URL

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.

2)将孩子张贴到〜/ Children,然后将此孩子的ID链接从此回复POST到URL~ / Parents(321)/ $ links / Children。

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

您可以参考此链接获取OData规范的这一部分。

#1


6  

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

是的,OData支持这一点,Web API OData也支持它。 OData称之为深插入。这是规范的链接。

Now there can be two types of deep inserts,

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

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

1)嵌套项也是新的,应该创建。 json有效负载的示例如下所示,

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

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,

2)嵌套项已经存在,父项是新项,必须链接到嵌套项。 OData协议调用此绑定。示例负载看起来像,

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

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.

Web API OData支持第一种深度插入,并且不支持绑定(第二个示例)。如果是第一个有效负载,您的控制器将收到一个父对象,其中正确填充了Children集合。

And regarding your second question, you have two options,

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

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

1)将孩子张贴到〜/ Parents(321)/ Children URL

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.

2)将孩子张贴到〜/ Children,然后将此孩子的ID链接从此回复POST到URL~ / Parents(321)/ $ links / Children。

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

您可以参考此链接获取OData规范的这一部分。