I’m trying to do something very simple with the latest WebAPI. I want to post simple string content. My client code looks like:
var result = client.PostAsync(address, new StringContent(message)).Result;
My server code looks like:
[HttpPost]
public void JobHistory(string userId, string jobId, [FromBody] string message)
{
Console.WriteLine("[TestController.JobHistory] JuserId '{0}', jobId '{1}': {2}", userId, jobId, message);
}
The exception I get (which results in 500) is:
No MediaTypeFormatter is available to read an object of type 'String' from content with media type 'text/plain'.
After talking with the WebAPI folks, there are a couple workarounds but they require dropping down a level in the API (either write a customer formatter or grab the request object in the controller and invoke ReadAsStringAsync()). Since the client API supports sending string content without any custom work, I think the server should have a default formatter for text/plain as well.
Comments: The MediaTypeFormatter model exists so that it is easy to extend the supported set of media types within a Web API application. While a plain/text is interesting we don't consider it sufficiently mainstream to include it in the core package. Rather we would encourage to either pick up a plain text formatter from a sample or from a contrib project.