我正在使用 swagger ui 插件来记录我的 web api。我想返回 JSON 对象,如:
{"Person": {
"Id": 1,
"Name": "John",
"address": {
"Street": "ABC",
"City": "Penrith",
"PostCode": 2034,
"State": "NSW"
},
"DOB": "2013-11-11T00:00:00"
}
}
注意 Person 对象名称。
我可以使用以下代码执行此操作:
public HttpResponseMessage Get(int id)
{
Person person = new Person { Id = 1, Name = "John", DOB = new DateTime(2013, 11, 11), address = new Address { City = "Penrith", State = "NSW", PostCode = 2034, Street = "ABC" } } ;
return Request.CreateResponse(HttpStatusCode.Accepted, new { Person = person });
}
不幸的是,因为返回类型是 HttpResponseMessage 而不是 Person 本身,所以 Swagger 只是将模型显示为 HttResponseMessage。那不是我想要的。
如果我将返回类型更改为 Person 并返回一个 person 对象,我不会在 JSON 返回中获得 Person 对象名称。那只返回 -
{
"Id": 1,
"Name": "John",
"address": {
"Street": "ABC",
"City": "Penrith",
"PostCode": 2034,
"State": "NSW"
},
"DOB": "2013-11-11T00:00:00"
}
有没有办法返回 Person 但有带有 Person 对象名称的 JSON 字符串?