首页 > 解决方案 > 如何查看从 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

在此处输入图像描述

标签: asp.net-mvchttpasp.net-web-apihttpresponsehttpresponsemessage

解决方案


如果您要实现的是返回有效的 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"}

推荐阅读