相关疑难解决方法(0)

为什么整个HttpResponseMessage序列化?

为什么要使用这个Web API

[HttpGet("hello")]
public HttpResponseMessage Hello()
{
    var res = new HttpResponseMessage(HttpStatusCode.OK);
    res.Content = new StringContent("hello", Encoding.UTF8, "text/plain");
    return res;
}
Run Code Online (Sandbox Code Playgroud)

返回

{
  "Version":{
    "Major":1,
    "Minor":1,
    "Build":-1,
    "Revision":-1,
    "MajorRevision":-1,
    "MinorRevision":-1
  },
  "Content":{
    "Headers":[{
      "Key":"Content-Type",
      "Value":["text/plain; charset=utf-8"]
    }]
  },
  "StatusCode":200,
  "ReasonPhrase":"OK",
  "Headers":[],
  "RequestMessage":null,
  "IsSuccessStatusCode":true
}
Run Code Online (Sandbox Code Playgroud)

代替

hello
Run Code Online (Sandbox Code Playgroud)

如何让Web API返回如下所示的HTTP响应?

200 OK
Content-Type: text/plain

hello
Run Code Online (Sandbox Code Playgroud)

我最终想要做的是返回JSON和其他具有各种状态代码的格式,因此以下代码不能帮助我作为答案.

[HttpGet("hello")]
public string Hello()
{
    return "hello";
}
Run Code Online (Sandbox Code Playgroud)

(我是ASP.NET和其他Microsoft技术的新手.)

c# asp.net asp.net-web-api

5
推荐指数
1
解决办法
1409
查看次数

标签 统计

asp.net ×1

asp.net-web-api ×1

c# ×1