1

I would like to understand why in case when there is only one parameter in the POST body, for content type url-formencoded the syntax is different than in case of multiple parameters.

Let's assume we've got these 2 methods with matching route templates:

public string PostSingleString([FromBody]string value)
{
    return value;
}

public class Values
{
    public string Value1 {get; set;}
    public string Value2 {get; set;}
}

public string PostMultipleStrings(Values values)
{
    return String.Format("{0}-{1}", values.Value1, values.Value2);
}

In the first case the request would be:

POST    [controller]/
body: =myValue

in the second one though:

POST    [controller]/
body: Value1=one&Value2=two

If in the first case the syntax from the second example is used (value=myValue) the param won't bind (unless the string is wrapped in a complex type).

Can anyone explain why this is the case?

Joanna Derks
  • 4,033
  • 3
  • 26
  • 32
  • I think only who is working in the Web.API team can answer why it is like this. You should accept the fact this is how it is [implemented](http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-1#sending_simple_types) – nemesv May 08 '13 at 19:50
  • @nemesv - I know that, I'm looking more for pointers to WebApi codebase, to a piece of code there that would explain why this inconsistency was a requirement. – Joanna Derks May 08 '13 at 19:54
  • 1
    You can check this [discussion](http://aspnetwebstack.codeplex.com/discussions/359687). Anyway I think the [Wep.API discussion](http://aspnetwebstack.codeplex.com/discussions/topics/5322/asp-net-web-api) forum is a better fit for your question than SO. – nemesv May 08 '13 at 20:01
  • 1
    It has to do with Model Binding vs. using Formatters (and the fact that only a single parameter can come from the body). Mike Stall has a good blog entry here about it: http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx and this other stackoverflow question has some good information too: http://stackoverflow.com/questions/14624306/web-api-parameter-always-null. – Jason Haley May 09 '13 at 01:15

0 Answers0