Webapi GET应返回ID或值

时间:2017-05-06 21:59:43

标签: c# rest asp.net-web-api

最近我开始为我们的应用程序设计web api。我在this book找到了许多好的提示,但我找不到有关回复内容的问题的答案。让我们说我有以下模型

class Foo
{
   public int Id {get;set;}
   public string Name {get;set;}
   public int BarId {get;set;}
}

class Bar
{
   public int Id {get;set;}
   public string Name {get;set;}
}

当用户请求获取Foo时,WebApi应该从BarId Name模型返回BarBarId吗?

所以结果应该是

{
  "id":1,
  "name":"foo",
  "barId":100
}

{
  "id":1,
  "name":"foo",
  "barName":"bar"
}

可能应该返回id和value吗?任何建议都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

这取决于用户界面,他们需要展示什么。

最好发送barId和barname,以便在api中发送所有信息

{
  "id":1,
  "name":"foo",
  "barId":2,
  "barName":"bar"
}
相关问题