我如何/是否可以将 json 对象传递给 webapi 控制器(POST)并且没有将其映射到的类,而是将其作为任意内容处理?
因此,如果我像这样从客户那里传递信息:
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);
}
});
}
和数据是这样的:
{ "_id" : ObjectId("5069f825cd4c1d590cddf206"), "firstName" : "John", "lastName" : "Smith", "city" : "Vancouver", "country" : "Canada" }
我的控制器将能够解析它吗?下次数据可能与该签名不匹配(例如:
{ "_id" : ObjectId("5069f825cd4c1d56677xz6"), "company" : "Acme" }
在我的控制器中,我尝试过:
public HttpResponseMessage Post([FromBody]JObject value)
和:
public HttpResponseMessage Post([FromBody]string value)
并且(因为这实际上是使用 mongo db):
public HttpResponseMessage Post([FromBody]BsonDocument value)
但看起来对象映射器想要映射到字符串以外的东西......