WebApi bind body to Json dictionary

半腔热情 提交于 2019-12-20 05:43:09

问题


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:


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



来源:https://stackoverflow.com/questions/15486763/webapi-bind-body-to-json-dictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!