简体   繁体   English

如何在Web API OData中发布具有导航关系的实体?

[英]How to POST an entity complete with navigational relationship in Web API OData?

I'm using ASP.NET Web API with OData. 我正在将ASP.NET Web API与OData一起使用。 I'm trying to POST a Child entity, which has a relationship to a Parent (the parent already exists). 我正在尝试发布与父级 (父级已经存在)有关系的子级实体。 When I post the entity (using WCF data service client and SetLink), I can see via Fiddler that it is adding a <link...href=[address of parent]> into the body of the request. 当我发布实体(使用WCF数据服务客户端和SetLink)时,我可以通过Fiddler看到它正在向请求的主体中添加<link...href=[address of parent]> This exact same request works against our WCF data service version of the service (we are migrating to Web API). 此完全相同的请求适用于该服务的WCF数据服务版本(我们正在迁移到Web API)。 However, this does not seem to translate anything into the Post method on the controller in Web API. 但是,这似乎没有将任何内容转换为Web API中控制器上的Post方法。

When posting the child to ChildController, how can I access the Parent's ID from the Post action on ChildController ? 将孩子发布到ChildController时,如何通过ChildController上的Post操作访问父母的ID? I know the value is there in the request, but how can I get ahold of this value? 我知道请求中存在该值,但是如何获得该值? The Child cannot be created without a Parent. 没有父母就不能创建孩子。 Do I need to modify the controller action signature? 我是否需要修改控制器动作签名? Maybe there is some attribute I can use somewhere? 也许我可以在某处使用某些属性? From an API perspective, I would like to avoid adding ParentId directly to the Child entity if possible. 从API角度来看,如果可能的话,我想避免将ParentId直接添加到Child实体。

public class ChildController
{
    public HttpActionResult Post([FromBody]Child child)
    {
        //child.Parent is null here, but all other 
        //properties of Child are populated.
        //How can I get the Parent's ID from the POST request??
    }
}

public class Child
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Parent Parent { get; set; }
}

public class Parent
{
    public int Id { get; set; }
    public IEnumerable<Children> Children { get; set; }
}

EDIT: Here is my request. 编辑:这是我的要求。 I changed some of the names to protect the innocent (replaced host name, and entity names with parent/child): 我更改了一些名称来保护无辜(替换的主机名和带有父/子的实体名称):

POST https://localhost/MyWebService/Child HTTP/1.1
Content-Type: application/atom+xml
DataServiceVersion: 1.0;NetFx
MaxDataServiceVersion: 3.0;NetFx
Accept: application/atom+xml,application/xml
Accept-Charset: UTF-8
User-Agent: Microsoft ADO.NET Data Services

Host: localhost
Content-Length: 1048
Expect: 100-continue

<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<category term="MyWebService.Entities.Child" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Parent" type="application/atom+xml;type=entry" title="Parent" href="https://localhost/MyWebService/Parents(1L)" />
<id />
<title /><updated>2014-05-30T16:07:28Z</updated><author><name /></author>
<content type="application/xml">
<m:properties>
<d:Content>content</d:Content>
<d:CreatedDate m:type="Edm.DateTime">0001-01-01T00:00:00</d:CreatedDate>
<d:Description>desc</d:Description>
<d:Enabled m:type="Edm.Boolean">true</d:Enabled>
<d:Id m:type="Edm.Int64">0</d:Id><d:TabName>tname</d:TabName>
</m:properties>
</content>
</entry>

In order to post an entity from navigation link, you need to define your action in Parent Controller. 为了从导航链接发布实体,您需要在父控制器中定义操作。 Here is the code snippet: 这是代码片段:

public class ParentController
{
    public HttpActionResult PostToChildren(int key, [FromBody]Child child)
    {
        var parent = parents.single(p=>p.Id == key);
        if(parent != null)
        {
            parent.Children.Add(child);
            ChildController.Children.Add(child);
            return StatusCode(HttpStatusCode.NoContent);
        }
        else
            return BadRequest();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM