【发布时间】:2014-08-18 21:23:05
【问题描述】:
我正在使用 EF 6.0 Code First、AutoMapper 和 WebApi 2.0 开发 REST API。
我正在努力序列化数据,或者更具体地说;选择响应请求时应序列化的内容。
假设我正在为具有以下课程的足球联赛创建一个应用程序:
public class Player
{
public String Name { get; set; }
public virtual ICollection<Team> Teams { get; set; } // example
}
public class Team
{
public String Name { get; set; }
public virtual ICollection<Team> Players { get; set; }
public virtual ICollection<FootballGame> Games { get; set; }
}
public class FootballGame
{
public Team Team1 { get; set; }
public Team Team2 { get; set; }
public DateTime DateTime { get; set; }
}
我有一个名为 PlayersController 的 API 控制器,以及一个应该返回该播放器的普通 Get 方法 url/api/players/id。
但是,由于 LazyLoading、序列化和多对多关系,我返回了与该播放器相关的每个对象:
{
"$id": "1",
"Id": 1,
"Name": "Jon Jones",
"Teams": [
{
"$id": "2",
"Id": 1,
"Name": "Liverpool",
"Players": [
{
"$id": "3",
"Id": 2,
"Name": "James White",
"Teams": [
{
"$ref": "2"
},
{
"$id": "4",
"Id": 2,
"Name": "Man Utd",
"Players": [
{
"$ref": "3"
},
{
"$id": "5",
"Id": 3,
"Name": "John Snow",
"Teams": [
{
"$ref": "2"
},
{
"$ref": "4"
}
]
},
{
"$ref": "1"
}
],
"Games": [
{
"$id": "6",
"Id": 1,
"Team1": {
"$ref": "2"
},
"Team2": {
"$ref": "4"
},
"DateTime": "2014-08-18T23:20:01.7797942+02:00"
},
{
"$id": "7",
"Id": 2,
"Team1": {
"$id": "8",
"Id": 3,
"Name": "Man City",
"Players": [
{
"$ref": "1"
},
{
"$ref": "3"
},
{
"$id": "9",
"Id": 4,
"Name": "Elton John",
"Teams": [
{
"$ref": "2"
},
{
"$ref": "8"
}
]
}
],
"Games": [
{
"$ref": "7"
},
{
"$id": "10",
"Id": 5,
"Team1": {
"$ref": "2"
},
"Team2": {
"$ref": "8"
},
"DateTime": "2014-08-18T23:20:01.7807943+02:00"
}
]
},
"Team2": {
"$ref": "4"
},
"DateTime": "2014-08-18T23:20:01.7807943+02:00"
}
]
},
{
"$ref": "8"
}
]
},
{
"$ref": "5"
},
{
"$ref": "9"
}
],
"Games": [
{
"$ref": "6"
},
{
"$ref": "10"
}
]
},
{
"$ref": "4"
}
]
}
我怎样才能以一种简单的方式选择只返回球员,而对于球队,而不是返回球队可以获得数据的网址,例如:
{
"Id": 1,
"Name": "Jon Jones",
"Teams": "http://url/api/player/1/teams/"
}
编辑: 通过按照建议禁用延迟加载,我得到:
{
"Id": 1,
"Name": "Jon Jones",
"Teams": []
}
如果这是要走的路,是否有“最佳实践”来添加 uri 以让团队进入那里?
【问题讨论】:
标签: c# rest json.net asp.net-web-api asp.net-web-api2