2
我想在我的API中将外键对象嵌入到我的.NET JSON响应中。我来自一个Rails背景并且格式化JSON响应,以包含任何找到的外键记录都非常简单。但是,我似乎无法找到办法做到这一点。我有一个外键关系建立像下面的模型:在.NET中嵌入JSON响应中的外键对象
public class Person
{
[Key]
public int id {get; set;}
public string name {get; set;}
}
public class House
{
[Key]
public int id {get; set;}
public string address {get; set;}
[Required]
public int personId {get; set;}
[ForeignKey("peronsId")]
public Person person {get; set;}
}
然而,当我运行一个GET请求,我得到的形式殿
{
"id":1,
"address":"Middle of Nowhere",
"personId":3
}
当我要的是:
{
"id":1,
"address":"Middle of Nowhere",
"person":{
"id":3,
"name":"Some Dude"
}
}
或者一些类似的结构。我怎样才能在.NET中完成这项工作?
在C#中使用.NET 4.5 Web API 2与实体框架。