How do I / is it possible to pass in a json object to a webapi controller (POST) and not have a class to map it to, but rather handle it as arbitrary content?
如何将json对象传递给webapi控制器(POST)而没有类来映射它,而是将其作为任意内容来处理?
So if I pass in from my client like so:
如果我从我的客户那里经过
createRecord: function (model, data, callback, callbackParams) {
var request = jQuery.ajax({
type: "POST", // default = GET,
url: '/api/' + model + '/',
data: data,
contentType: 'application/json',
success: function (msg) {
$('#results').text(msg);
if (callback) // only fire a callback if it has been specified
callback(msg, callbackParams);
},
error: function (jqXHR, textStatus) {
alert('Request failed: ' + textStatus);
}
});
}
and data is something like:
数据是这样的:
{ "_id" : ObjectId("5069f825cd4c1d590cddf206"), "firstName" : "John", "lastName" : "Smith", "city" : "Vancouver", "country" : "Canada" }
My controller will be able to parse it? And next time the data may not match that signature (eg:
我的控制器能解析它吗?下次数据可能与签名不匹配(例如:
{ "_id" : ObjectId("5069f825cd4c1d56677xz6"), "company" : "Acme" }
In my controller, I have tried:
在我的控制器中,我尝试过:
public HttpResponseMessage Post([FromBody]JObject value)
and:
和:
public HttpResponseMessage Post([FromBody]string value)
and (because this is actually to work with a mongo db):
而且(因为这实际上是用mongo db做的):
public HttpResponseMessage Post([FromBody]BsonDocument value)
but it looks like the object mapper wants to map to something other than string...
但是看起来对象映射器想要映射到字符串之外的东西……
4 个解决方案
#1
23
You can have your post method that takes in a HttpRequestMessage to by pass the model binding logic and you can read the content of the request directly:
通过传递模型绑定逻辑,您可以让post方法接收HttpRequestMessage,并可以直接读取请求的内容:
public HttpResponseMessage Post(HttpRequestMessage req)
{
var data = req.Content.ReadAsStringAsync().Result; // using .Result here for simplicity...
...
}
By the way, the reason why the action that takes in JObject doesn't work is because of 'ObjectId("...")' that is used as the value of "_id" in your data...
顺便说一下,在JObject中执行的操作失败的原因是'ObjectId("…")',它被用作数据中的“_id”的值……
#2
18
We passed json object by jquery, and parse it in dynamic object. it works fine. this is sample code:
我们通过jquery传递json对象,并在动态对象中解析它。它将正常工作。这是示例代码:
ajaxPost:
...
Content-Type: application/json,
data: {
"name": "Jack",
"age": "12"
}
...
webapi:
webapi:
[HttpPost]
public string DoJson2(dynamic data)
{
string name = data.name;
int age = data.age;
return name;
}
similary question on *: WebAPI Multiple Put/Post parameters
关于*的类似问题:WebAPI多个Put/Post参数
#3
2
In your input, "_id": ObjectId("5069f825cd4c1d590cddf206")
is what is breaking the JSON materialization on the server. Removing ObjectId
and using "_id" : "5069f825cd4c1d590cddf206"
works with JObject
as well as Dictionary<string, object>
在您的输入中,“_id”:ObjectId(“5069f825cd4c1d590cddf206”)破坏了服务器上的JSON物化。删除ObjectId并使用“_id”:“5069f825cd4c1d590cddf206”与JObject以及Dictionary
#4
-6
It is very easy, you just need to put the Accept Header to "application/json".
这非常简单,只需将Accept标头放到“application/json”中。
#1
23
You can have your post method that takes in a HttpRequestMessage to by pass the model binding logic and you can read the content of the request directly:
通过传递模型绑定逻辑,您可以让post方法接收HttpRequestMessage,并可以直接读取请求的内容:
public HttpResponseMessage Post(HttpRequestMessage req)
{
var data = req.Content.ReadAsStringAsync().Result; // using .Result here for simplicity...
...
}
By the way, the reason why the action that takes in JObject doesn't work is because of 'ObjectId("...")' that is used as the value of "_id" in your data...
顺便说一下,在JObject中执行的操作失败的原因是'ObjectId("…")',它被用作数据中的“_id”的值……
#2
18
We passed json object by jquery, and parse it in dynamic object. it works fine. this is sample code:
我们通过jquery传递json对象,并在动态对象中解析它。它将正常工作。这是示例代码:
ajaxPost:
...
Content-Type: application/json,
data: {
"name": "Jack",
"age": "12"
}
...
webapi:
webapi:
[HttpPost]
public string DoJson2(dynamic data)
{
string name = data.name;
int age = data.age;
return name;
}
similary question on *: WebAPI Multiple Put/Post parameters
关于*的类似问题:WebAPI多个Put/Post参数
#3
2
In your input, "_id": ObjectId("5069f825cd4c1d590cddf206")
is what is breaking the JSON materialization on the server. Removing ObjectId
and using "_id" : "5069f825cd4c1d590cddf206"
works with JObject
as well as Dictionary<string, object>
在您的输入中,“_id”:ObjectId(“5069f825cd4c1d590cddf206”)破坏了服务器上的JSON物化。删除ObjectId并使用“_id”:“5069f825cd4c1d590cddf206”与JObject以及Dictionary
#4
-6
It is very easy, you just need to put the Accept Header to "application/json".
这非常简单,只需将Accept标头放到“application/json”中。