'.Net Framework 4.6.1 - Web API - HttpResponseMessage.Content is empty for StringContent
Content being returned by HttpMessageResponse is blank/empty after being explicitly set
I have created a WebApi with the latest .net Framework 4.6.1 and am trying to pass back some TwiML back to Twilio (not really the issue/problem as I have the problem even with plain text).
For some reason, no matter what I do, when setting the Content for a HttpResponseMessage that content is not passed back to the caller. I have used Postman and Fiddler, and while the content header is showing after setting the content via StringContent, the actual content I am trying to pass back does not. It does however seem to be showing in the ContentLength.
I am really confused and may be overlooking something very simple, but over a day has been wasted so far.
I am showing just text trying to be sent back with nothing fancy and I get nothing back! In my real use case I am trying to pass back a string of XML as a type of text/xml. But even a basic text string does not come back!!
Any help is greatly appreciated, this has totally stumped me.
public HttpResponseMessage Post(TwilioCall call)
{
...
...
...
var responseMessage = new HttpResponseMessage
{
Content = new StringContent("Representative Text Here")
};
return responseMessage;
}
The following is returned back to Postman, you can see the actual content is blank:
{
"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
}
That was the pretty version. Here is what the response looks like raw. {"Version":"Major":1,"Minor":1,"Build":-1,"Revision":-1,"MajorRevision":-1,"MinorRevision":-1},"Content":{"Headers":[{"Key":"Content-Type","Value":["text/xml; charset=utf-8"]}]},"StatusCode":200,"ReasonPhrase":"OK","Headers":[],"RequestMessage":null,"IsSuccessStatusCode":true}
Another example of just a very plain vanilla post response:
:(
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using System.Net.Http;
using System.Text;
using MyNamespace.Models;
namespace MyNamespace.Controllers
{
[Route("[controller]")]
public class BasicTextResponseController : Controller
{
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// POST api/values
[HttpPost]
public HttpResponseMessage Post(TwilioCall call)
{
var responseMessage = new HttpResponseMessage
{
Content = new StringContent("Text to return.")
};
return responseMessage;
}
}
}
Solution 1:[1]
Instead of creating the HttpResponseMessage type directly, try:
var response = Request.CreateResponse<String>(HttpStatusCode.Created, "OK");
return response;
Also, if you are doing some sort of post, you can set the response headers to the location of the new item (like a PRG - post, redirect, get pattern):
response.Headers.Location = new Uri(...);
then return the response.
See if that works.
---------EDIT------------
You might have some other issues? Did you create your Controller from scratch? You should inherit from ApiController (and NOT Controller) for starters. The ApiController has different plumbing over the standard MVC Controller.
As for verification of the missing Request.CreateResponse() method, I don't know what version of studio you're using, but I went ahead and ran a repair on framework 4.6.1 just to assert that I have a good install. Since I'm using VS 2012, I also went ahead and downloaded framework 4.6 targeting pack. This allows me to select framework 4.6.1 when I created a local test webapi project.
Here's one of the method signatures for CreateResponse():
// Summary:
// Creates an System.Net.Http.HttpResponseMessage wired up to the associated
// System.Net.Http.HttpRequestMessage.
//
// Parameters:
// request:
// The HTTP request message which led to this response message.
//
// statusCode:
// The HTTP response status code.
//
// value:
// The content of the HTTP response message.
//
// Type parameters:
// T:
// The type of the HTTP response message.
//
// Returns:
// An initialized System.Net.Http.HttpResponseMessage wired up to the associated
// System.Net.Http.HttpRequestMessage.
public static HttpResponseMessage CreateResponse<T>(this HttpRequestMessage request, HttpStatusCode statusCode, T value);
The using namespace is System.Net.Http; but this shouldn't be confused with the System.Net.Http.dll as these extensions reside in System.Web.Http.dll.
I hope that help. Perhaps I might have a few points returned back to me for the follow up.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |