我的API控制器方法是
public IQueryable<Team> GetTeam([FromUri] string include = "")
{
return from c in db.Team select new Team { id = c.TeamID, name = c.teamName, logo = "", social = new Map.Social { facebook = "", twitter = "" } };
}
上述方法的反应是
[{"id":1,"name":"example","logo":"","social":{"twitter":"","facebook":""}}]
问题是如果方法接受“social”作为include
参数,则应创建上述响应。
如果include
参数为null
,则响应应为
[{"id":1,"name":"example","logo":""}]
我应该如何更改上面的代码才能实现?
使用的课程:
public class Team
{
public int id { get; set; }
public string name { get; set; }
public string logo { get; set; }
public Social social { get; set; }
}
public class Social
{
public string twitter { get; set; }
public string facebook { get; set; }
}
答案 0 :(得分:1)
您的问题的措辞解释了需要编程的内容。
一个简化的例子是
public IQueryable<Team> GetTeam([FromUri] string include = "") {
//If the include parameter is null the response should be
if(String.IsNullOrWhiteSpace(include) {
return from c in db.Team select new Team { id = c.TeamID, name = c.teamName, logo = ""};
}
return from c in db.Team select new Team { id = c.TeamID, name = c.teamName, logo = "", social = new Map.Social { facebook = "", twitter = "" } };
}