【问题标题】:How to view content delivered from HttpResponseMessage (ASP.NET MVC Web Api)如何查看从 HttpResponseMessage (ASP.NET MVC Web Api) 传递的内容
【发布时间】:2019-03-31 00:47:13
【问题描述】:

我创建 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-mvc http asp.net-web-api httpresponse httpresponsemessage


【解决方案1】:

如果你想要实现的是返回一个有效的 JSON 响应,那么这就是 Asp.Net MVC 的方式

    public ActionResult HttpResponseMessage()
    {
        var oJSON = new { url = "path_to_file", hash = "aaaaaaaaaaaaaaaaa" };
        return Json(oJSON, JsonRequestBehavior.AllowGet);
    }

Postman 看到的响应标头:

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==?=

Postman 看到的响应正文

{"url":"path_to_file","hash":"aaaaaaaaaaaaaaaaa"}

【讨论】:

    猜你喜欢
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    • 2023-03-11
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多