ChaseMedallion
ChaseMedallion

Reputation: 21764

WebApi bind body to Json dictionary

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?

Upvotes: 1

Views: 8752

Answers (1)

Youssef Moussaoui
Youssef Moussaoui

Reputation: 12395

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".

Upvotes: 3

Related Questions