WebApi将主体绑定到Json词典 [英] WebApi bind body to Json dictionary
问题描述
我正在尝试创建具有以下签名的WebApi操作方法:
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)
但是,当我在请求中使用此方法时,参数永远不会正确绑定(两个URI字段可以正确绑定). ModelState在第一个字符处显示Json.NET解析错误.我试过了如下所示的请求主体:id=50
和arguments={ "id": 50 }
.我该如何制定我的请求以允许WebApi正确绑定我的参数?
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?
推荐答案
您在请求正文中不需要"id =或"arguments =".您应该能够发送如下所示的内容:
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"}}
并使其正常工作.然后,字典将包含key1:一个值为4的JValue,key2:一个值为50的JValue,key3:一个member1成员的值为"value"的JObject.
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".
这篇关于WebApi将主体绑定到Json词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!