Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jun 07, 2012 10:16 PM by MikeWasson
Jun 07, 2012 07:42 PM|LINK
I have made my way through XML and JSON so far.
Also with 2012 RC we can respond with plain text which is no longer decorated with html, json or XML, but is sent back as simple text/plain.
Now I need to post a simple text, not wrapped in any xml or json.
When we create a new API Controller, the post is defined like this.
However, there is no way we can post plain text to it by deffining Content-Type: text/plain; charset=utf-8.
The method receives the call, but the value is always empty.
I created my own formatter for text/plain and I made sure to add it to the end of the formatters list.
However my formatter never receives any call, and the method still receives null value.
The funny thing is that if I put my plain text formatter at the beginning of the formatters list, and
change my method to receive a specific class (model) instead of string and send it from the client as application/xml, my text formatter is invoked.
So what's the point of specifying "SupportedMediaTypes.Add(new
the formatter is invoked for application/xml? And why is my formatter NEVER invoked when I specify text/plain conetnt type?
What am I missing here?
Jun 07, 2012 08:04 PM|LINK
Try adding the FromBody attribute to the parameter:
public void Post([FromBody] string value)
By default, Web API tries to get simple parameter types from the request URI, and complex types from the message body.
Jun 07, 2012 08:20 PM|LINK
Thanks Mike, you scored again!
Any of this documented womewhere by anychance?
Jun 07, 2012 10:16 PM|LINK
Mike Stall's blog has some good info on this:
I have a work-item to write up something about parameter binding for the asp.net/web-api docs. :-)