从web api帖子返回text\plain json响应

Ami*_*ich 2 c# json asp.net-web-api

我在从WebApi返回'text/plain'json响应时遇到问题.
我需要返回它text/plain,默认情况下它将它返回为application/json.
我遇到的问题正是这个链接中发生的事情.这不是一个重复的帖子,因为我阅读了多个帖子,但仍然有一个小问题.

这是我的服务器代码:

public HttpResponseMessage Post()
{
       var response = GetModel();
       string jsonRes = JsonConvert.SerializeObject(response);

       var resp = new HttpResponseMessage();
       resp.Content = new StringContent(jsonRes);
       resp.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");

       return resp;
}
Run Code Online (Sandbox Code Playgroud)

这是我得到的回应:

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
  Content-Type: text/plain
}
Run Code Online (Sandbox Code Playgroud)

jsonRes var包含有效的JSON.
为什么内容下载为内容:System.Net.Http.StringContent

我可能在这里错过了一些小东西.

syl*_*ter 6

你可以这样做:

  public string Post()
    {
           var response = GetModel();
           string jsonRes = JsonConvert.SerializeObject(response);


           return jsonRes ;
    }
Run Code Online (Sandbox Code Playgroud)