1

I'm trying to create a WebApi action method with the following signature:

[System.Web.Http.HttpPost] public object Execute([FromUri] string command, [FromUri] string method, [FromBody] IDictionary<string, JToken> arguments) 

However, when I hit this method with requests, arguments never binds correctly (the two URI fields do). The ModelState shows a Json.NET parse error at the first character. I've tried request bodies that look like: id=50 and arguments={ "id": 50 }. How do I have to formulate my request to allow WebApi to correctly bind my parameters?

1
  • 1
    Dictionaries consist of keyValuePairs, thus in the json world an array of objects, each having a Key and Value property, so IDictionary<string,int> binding requries json something like arguments=[{Key:"id",Value:50},{Key:"another",Value:100}]. Might be off slightly, if you serialized a Dictionary as a JSON object you'd see what I mean. Commented Mar 18, 2013 at 21:03

1 Answer 1

3

You don't need the "id=" or "arguments=" in the request body. You should be able to just send something that looks like this:

{"key1": 4, "key2": 50, "key3": {"member1": "value"}} 

and have it work. The Dictionary would then contain key1: a JValue with value 4, key2: a JValue with value 50, key3: a JObject with a member1 member with value "value".

Sign up to request clarification or add additional context in comments.

1 Comment

Do AJAX libraries like JQuery and YUI support posting data in this format?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.