Làm thế nào Web API có thể không deserialize một đối tượng mà JSON.Net deserializes?Tại sao API Web không deserialize này nhưng JSON.Net sẽ?
Đây là bộ điều khiển Web API:
public void Put(EditorSubmissionMainView ajaxSubmission) {
// ajaxSubmission: EditorSubmissionMainView with all values ('data' also == null)
string json = "{\"id\":\"row_1377\",\"data\":{\"ROTATION\":\"1\",\"EQUIPMENT\":[{\"id\":\"6\"},{\"id\":\"8\"}],\"NOTES\":\"\"}}";
EditorSubmissionMainView foo = Newtonsoft.Json.JsonConvert.DeserializeObject<EditorSubmissionMainView>(json) as EditorSubmissionMainView;
// foo is a EditorSubmissionMainView but properly deserialized.
}
Đây là JSON, bị bắt bởi Fiddler và định dạng:
{
"id": "row_1377",
"data": {
"ROTATION": "1",
"EQUIPMENT": [{
"id": "6"
},
{
"id": "8"
}],
"NOTES": ""
}
}
Một lớp ví dụ mà serializes với JSON.Net nhưng không phải với bộ điều khiển API Web:
[Serializable]
public class EditorSubmissionMainView
{
public string id { get; set; }
public EditorSubmissionMainViewData data { get; set; }
}
[Serializable]
public class EditorSubmissionMainViewData
{
[JsonProperty("ROTATION")]
public int? rotation { get; set; } // Same problem if everything is a string
[JsonProperty("EQUIPMENT")]
public ICollection<Dictionary<string, int?>> equipment { get; set; }
[JsonProperty("NOTES")]
public string notes { get; set; }
}
API Web sử dụng JSON.Net và tôi không sử dụng bất kỳ trình định dạng JSON tùy chỉnh nào - chỉ cần chuyển JSON sang bộ điều khiển API Web. Tại sao cái này không hoạt động?
EDIT: Theo yêu cầu, tôi đang gọi bộ điều khiển API Web của tôi với Javascript này (một phần của JQuery DataTables). Lưu ý rằng tôi chắc chắn cùng JSON là nhận được để điều khiển của tôi bất kể vì tôi đã bắt các gói tin HTTP liệu với Fiddler và đảm bảo rằng nó là đúng:
"ajaxUrl": {
"create": "POST @Url.Content("~/API/MainView")",
"edit": "PUT @Url.Content("~/API/MainView")",
"remove": "DELETE @Url.Content("~/API/MainView")"
},
"ajax": function (method, url, data, successCallback, errorCallback) {
$.ajax({
"type": method,
"url": url,
"data": JSON.stringify(data), // Requires IE8+
"contentType": "application/json",
"dataType": "json",
"success": function (json) {
successCallback(json);
},
"error": function (xhr, error, thrown) {
errorCallback(xhr, error, thrown);
}
});
},
Yêu cầu HTTP liệu sau:
PUT http://localhost:53367/API/MainView HTTP/1.1
x-requested-with: XMLHttpRequest
Accept-Language: en-us
Referer: http://localhost:53367/Manage/MainView
Accept: application/json, text/javascript, */*; q=0.01
Content-Type: application/json
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Host: localhost:53367
Content-Length: 306
Connection: Keep-Alive
Pragma: no-cache
Cookie: ASP.NET_SessionId=wqsghjrol20cszrxfzdm0qo4
{"id":"row_1377","data":{"ROTATION":"1","EQUIPMENT":[{"id":"6"},{"id":"8"}],"NOTES":""}}
Vui lòng đăng cách bạn gọi phương thức webapi của mình (mã js ajax)! – nemesv
Đảm bảo yêu cầu của bạn có tiêu đề 'Content-Type: application/json' – marcind
@nemesv: Xong. :) –