为什么 HttpResponseMessage 不显示其内容?
Why does HttpResponseMessage not show its content?
您好,我正在尝试使用 HttpResponseMessage
发送 Json
object。
即使在调试数据时看起来它被插入 Content
部分(存在 104
字节)当使用 Postman
检索 json 时,在 Content
部分没有数据,只有一个header.
JsonResponse
{
"version": {
"major": 1,
"minor": 1,
"build": -1,
"revision": -1,
"majorRevision": -1,
"minorRevision": -1
},
"content": {
"headers": [
{
"key": "Content-Type",
"value": [
"text/plain; charset=utf-8"
]
} ////why no data ??
]
},
"statusCode": 200,
"reasonPhrase": "OK",
"headers": [],
"requestMessage": null,
"isSuccessStatusCode": true
}
如您所见,content.I 没有重复使用与早期应用程序相同的代码,我没有得到这个 problem.Why 内容是否为空?
代码
private static List<User> users = new List<User> {
new User{ Id = 0, Age = 0, Name = "Failed"},
new User{ Id = 12, Age = 33, Name = "Daniel"},
new User{ Id = 13, Age = 33, Name = "Marian"},
};
[HttpGet]
[Route("/api/getusers")]
public async Task<HttpResponseMessage> GetUsers() {
await Task.Delay(1000);
var str = JsonConvert.SerializeObject(users);
return new HttpResponseMessage {
StatusCode = HttpStatusCode.OK,
Content = new StringContent(str, Encoding.UTF8)
};
}
ASP.NET Core 没有对 returning HttpResponseMessage
的内置支持。如果你想 return JSON,你可以使用 IActionResult
并让 ASP.NET Core 为你处理序列化。这是一个更新的示例:
public async Task<IActionResult> GetUsers() {
await Task.Delay(1000);
return Json(users);
}
此处有更多选项,适用于您希望允许的情况 content-negotiation, for example. Here's a version that supports content-negotiation using OkObjectResult
:
public async Task<IActionResult> GetUsers() {
await Task.Delay(1000);
return Ok(users);
}
如果您愿意,您甚至可以 return users
本身:
public async Task<List<User>> GetUsers() {
await Task.Delay(1000);
return users;
}
这应该足以让您入门 - 官方文档进一步解释了事情:Controller action return types in ASP.NET Core Web API。
您好,我正在尝试使用 HttpResponseMessage
发送 Json
object。
即使在调试数据时看起来它被插入 Content
部分(存在 104
字节)当使用 Postman
检索 json 时,在 Content
部分没有数据,只有一个header.
JsonResponse
{
"version": {
"major": 1,
"minor": 1,
"build": -1,
"revision": -1,
"majorRevision": -1,
"minorRevision": -1
},
"content": {
"headers": [
{
"key": "Content-Type",
"value": [
"text/plain; charset=utf-8"
]
} ////why no data ??
]
},
"statusCode": 200,
"reasonPhrase": "OK",
"headers": [],
"requestMessage": null,
"isSuccessStatusCode": true
}
如您所见,content.I 没有重复使用与早期应用程序相同的代码,我没有得到这个 problem.Why 内容是否为空?
代码
private static List<User> users = new List<User> {
new User{ Id = 0, Age = 0, Name = "Failed"},
new User{ Id = 12, Age = 33, Name = "Daniel"},
new User{ Id = 13, Age = 33, Name = "Marian"},
};
[HttpGet]
[Route("/api/getusers")]
public async Task<HttpResponseMessage> GetUsers() {
await Task.Delay(1000);
var str = JsonConvert.SerializeObject(users);
return new HttpResponseMessage {
StatusCode = HttpStatusCode.OK,
Content = new StringContent(str, Encoding.UTF8)
};
}
ASP.NET Core 没有对 returning HttpResponseMessage
的内置支持。如果你想 return JSON,你可以使用 IActionResult
并让 ASP.NET Core 为你处理序列化。这是一个更新的示例:
public async Task<IActionResult> GetUsers() {
await Task.Delay(1000);
return Json(users);
}
此处有更多选项,适用于您希望允许的情况 content-negotiation, for example. Here's a version that supports content-negotiation using OkObjectResult
:
public async Task<IActionResult> GetUsers() {
await Task.Delay(1000);
return Ok(users);
}
如果您愿意,您甚至可以 return users
本身:
public async Task<List<User>> GetUsers() {
await Task.Delay(1000);
return users;
}
这应该足以让您入门 - 官方文档进一步解释了事情:Controller action return types in ASP.NET Core Web API。