Request.CreateResponse versus response.Content?
13:34 25 Jul 2015

My code was

var response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(
                             JsonConvert.SerializeObject(data),
                             Encoding.UTF8, "application/json");
return response;

and it works fine by returning some json data.

Later I noticed that Request.CreateResponse() can accept a second parameter T value with the value being the content of the HTTP response message. So I tried to compress the above three lines into one line

return Request.CreateResponse(
             HttpStatusCode.OK, new StringContent(JsonConvert.SerializeObject(data), 
             Encoding.UTF8, "application/json"));

But it doesn't work as expected. It returns

{
  "Headers": [
    {
      "Key": "Content-Type",
      "Value": [
        "application/json; charset=utf-8"
      ]
    }
  ]
}

Did I misunderstand the second parameter of Request.CreateResponse()?

c# httpresponse