asp.net-mvc - 如何查看从 HttpResponseMessage (ASP.NET MVC Web Api) 传递的内容
问题描述
我创建 json 对象并将其分配给我的 HttpResponseMessage 实例的 StringContent。当我调用 Web API 操作时一切正常,结果是 200,内容长度应该是这样,但是如何找到内容本身,json 在哪里?我在浏览器和 Postman 中得到的是:
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
Content-Type: application/json
}
为什么这是而不是我的 json 字符串?
Content: System.Net.Http.StringContent
解决方案
如果您要实现的是返回有效的 JSON 响应,那么这就是 Asp.Net MVC 中的方法
public ActionResult HttpResponseMessage()
{
var oJSON = new { url = "path_to_file", hash = "aaaaaaaaaaaaaaaaa" };
return Json(oJSON, JsonRequestBehavior.AllowGet);
}
邮递员看到的响应标头:
Cache-Control →private
Content-Length →49
Content-Type →application/json; charset=utf-8
Date →Fri, 26 Oct 2018 13:31:44 GMT
Server →Microsoft-IIS/10.0
X-AspNet-Version →4.0.30319
X-AspNetMvc-Version →5.2
X-Powered-By →ASP.NET
X-SourceFiles →=?UTF-8?B?RTpcRXhhbSA3MCA0ODdcNzA0ODdcTVZDUm91dGVzXEhvbWVcSHR0cFJlc3BvbnNlTWVzc2FnZQ==?=
邮递员看到的响应正文
{"url":"path_to_file","hash":"aaaaaaaaaaaaaaaaa"}
推荐阅读
- java - 自定义treeSet类,迭代器怎么写
- r - 使用 apply 时参数不一致
- reactjs - 使用 React 钩子取消 HTTP 获取请求
- java - 在 Windows、Linux 和 Mac 中是否有特定于操作系统的 API 用于调用文件选择器对话框?
- hive - HBase 存储处理程序:UnknownProtocolException:未找到名称 AuthenticationService hbase:meta 的协处理器
- groff - 在命令行禁用 groff 断字
- javascript - 在 JavaScript 中解析 Promise
- plsql - Oracle APEX 在 PL/SQL 中调用 Dialogflow V2 - 使用 OAuth2 或 Google 服务帐户进行身份验证
- python - 使用 FiPy 围绕球体流动的斯托克斯边界条件
- python-3.x - 在 Python 中将所有输出行导出为 CSV,而不仅仅是最后一行