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.
That's great.
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.
public void
Post(string
value)
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(newMediaTypeHeaderValue("text/plain"))"
if
the formatter is invoked for application/xml? And why is my formatter NEVER invoked when I specify text/plain conetnt type?
Pierre Lebel
Member
9 Points
28 Posts
How to post text/plain
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.
That's great.
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.
public void Post(string value)
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 MediaTypeHeaderValue("text/plain"))"
if 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?
Thanks
MikeWasson
Member
486 Points
78 Posts
Microsoft
Re: How to post text/plain
Jun 07, 2012 08:04 PM|LINK
Try adding the FromBody attribute to the parameter:
By default, Web API tries to get simple parameter types from the request URI, and complex types from the message body.
Pierre Lebel
Member
9 Points
28 Posts
Re: How to post text/plain
Jun 07, 2012 08:20 PM|LINK
Thanks Mike, you scored again!
Any of this documented womewhere by anychance?
MikeWasson
Member
486 Points
78 Posts
Microsoft
Re: How to post text/plain
Jun 07, 2012 10:16 PM|LINK
Mike Stall's blog has some good info on this:
http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx
I have a work-item to write up something about parameter binding for the asp.net/web-api docs. :-)
- Mike