Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用OData返回.NET API上的嵌套对象_C#_Json_Asp.net Web Api_Nhibernate_Odata - Fatal编程技术网

C# 使用OData返回.NET API上的嵌套对象

C# 使用OData返回.NET API上的嵌套对象,c#,json,asp.net-web-api,nhibernate,odata,C#,Json,Asp.net Web Api,Nhibernate,Odata,我正在开发一个使用OData的.NETAPI,目前在将嵌套对象返回到客户端时遇到问题 一切似乎都在运行,即使我在响应之前在控制器上设置了一个断点。我可以看到将返回的Vendor对象以及所有信息都已正确填充,但是当我查看客户端接收到的JSON响应时,它只有基本类型和枚举,作为另一个对象的所有其他属性都不会序列化为JSON结果 public class Vendor : IEntity { public virtual int Id { get; set; } public virt

我正在开发一个使用OData的.NETAPI,目前在将嵌套对象返回到客户端时遇到问题

一切似乎都在运行,即使我在响应之前在控制器上设置了一个断点。我可以看到将返回的Vendor对象以及所有信息都已正确填充,但是当我查看客户端接收到的JSON响应时,它只有基本类型和枚举,作为另一个对象的所有其他属性都不会序列化为JSON结果

public class Vendor : IEntity
{
    public virtual int Id { get; set; }
    public virtual VendorAddress PrimaryAddress { get; set; }
    public virtual string VendorName { get; set; }
}

public class VendorAddress : IEntity
{
    public virtual int Id { get; set; }
    public virtual Vendor Vendor { get; set; }
    public virtual Address Address { get; set; }

}

 public class Address : IEntity
{
    public virtual int Id { get; set; }   
    public virtual string Line1 { get; set; }
    public virtual string Line2 { get; set; }     
    public virtual string Country { get; set; }
    public virtual string CountryCode { get; set; }
    public virtual string City { get; set; }
    public virtual string County { get; set; }
    public virtual string StateProvince { get; set; }
    public virtual string ZipCode { get; set; }    
}   

 public SingleResult<Vendor> Get([FromODataUri] int key)
{
    var result = _repository.Query<Vendor>().Where(a => a.Id == key);
    return SingleResult.Create(result);
}
公共类供应商:IEntity
{
公共虚拟整数Id{get;set;}
公共虚拟供应商地址主地址{get;set;}
公共虚拟字符串VendorName{get;set;}
}
公共类供应商地址:IEntity
{
公共虚拟整数Id{get;set;}
公共虚拟供应商{get;set;}
公共虚拟地址{get;set;}
}
公共类地址:IEntity
{
公共虚拟整数Id{get;set;}
公共虚拟字符串Line1{get;set;}
公共虚拟字符串Line2{get;set;}
公共虚拟字符串国家{get;set;}
公共虚拟字符串CountryCode{get;set;}
公共虚拟字符串City{get;set;}
公共虚拟字符串country{get;set;}
公共虚拟字符串{get;set;}
公共虚拟字符串ZipCode{get;set;}
}   
公共单结果获取([FromODataUri]int-key)
{
var result=\u repository.Query()。其中(a=>a.Id==key);
返回SingleResult.Create(结果);
}

基本上,我想返回供应商信息,包括JSON结果上的PrimaryAddress/地址信息,但似乎不知道如何返回。

供应商地址和
地址都是所谓的导航属性。
您可以在查询中使用
$expand
来获取它们。 首先将[EnableQuery]添加到Get方法中

[EnableQuery]
public SingleResult<Vendor> Get([FromODataUri] int key)
在你的

WebApiConfig.Register(HTTPConfiguration config)
方法

下面是一个测试服务的请求示例:

还有更多信息可供参考:


希望这能有所帮助。

您是从
ODataController
派生出来的吗?什么是存储库?另外,尝试从不可用的属性中删除
virtual
loading@Alonso:您编写了.NETAPI。我猜你指的是ASP.NETWebAPI,对吧@jpq:是的,这是一个ASP.NETAPI@T.S.我确实是从ODataController派生的,所有其他OData查询url的东西,比如$count、$select和其他东西似乎都工作正常。无法消除虚拟原因据我所知,它们是与NHibernate合作所必需的。我想,我知道你的问题在哪里。我只是针对实体框架编写了这样的服务。在EF中,我需要做的就是正确地设置模型关系。例如,我看不出你们的模型之间的关系在哪里?这里是hocus pocus-OData使用与实体框架相同的属性类。如果在模型上设置这些属性(如
[Key]
[ForeignKey]
),Odata model builder将创建所有需要的关系,您的JSON将正常运行。或者可以使用流畅的Odata语法映射它们。在我的例子中,因为我在EF的模型中设置了映射,所以OData在没有特殊映射的情况下工作,感谢到目前为止的所有帮助,但是我仍然在努力解决这个问题:(.我在控制器调用中添加了代码[EnableQuery(MaxExpansionDepth=3)],并且我成功地扩展了第一个导航属性,但没有使用/Vendor(1)?$expand=primaryAddress,但我使用/api/v2/odata/Vendors?&$select=id,vendorName,primaryAddress&$expand=primaryAddress,从这个请求中我获得了id属性,但似乎无法扩展primaryAddress内的其他导航以实际获取我需要的地址信息。好的,您能检查$expand=primaryAddress/Address而不是$expand吗=主地址($expand=Address)
config.Expand()
WebApiConfig.Register(HTTPConfiguration config)