1
我使用MVC 5,網頁API 2與實體框架6 VS 2012ModelBinding不反序列化JSON
我能夠成功調用正確的行動在我的web API控制器,但傳入的JSON是永遠反序列化,或者至少不正確。 modelBinding認識到請求中的json與Post
動作的參數Type匹配,但每個屬性都爲null或缺省值。
爲什麼json沒有被反序列化?我在這裏看不到任何問題。
這不是一個有效的網址,只是在我的本地
身體:
{
"scab" : {
"url" : "http://money.cnn.com/2013/12/18/news/companies/target-credit-card/index.html?iid=Lead",
"title" : "Target: 40 million credit cards compromised",
"description" : "The Secret Service is investigating a reported credit card data breach at discount retailer Target.",
"image" : {
"height" : 367,
"width" : 620,
"url" : "http://i2.cdn.turner.com/money/dam/assets/131128213407-black-friday-target-pa-620xa.jpg"
},
"category" : null
}
}
請求頭:
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:422
Content-Type:application/json
Cookie:.ASPXAUTH=DA97AB5D720AC3100F9698DEAED044403CC5BA266EA8F3361E72C183DA2FAD5EB76D0D9DC2AB6DE30507FF788B7D6607473F87B6EE0D28C043DA50508407ABA1FC7ADA9B8A61F2A7C95024869064EE5D9C6863C670FEF221F9447A3D1F2E7CA67849A33B45B0776FFF0D76CD5C290500
Host:www.scabs.com
Origin:chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
控制器:
public class ScabsController : ApiController {
private ScabStore _store;
private ScabStore store {
get { return _store = _store ?? new ScabStore(); }
}
// POST api/scabs
public void Post(Scab scab) {
store.scabRepository.save(scab);
store.commit();
}
}
赤黴病類型
public class Scab {
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
[DataType(DataType.Url)]
public string url { get; set; }
public string title { get; set; }
public string description { get; set; }
public string site { get; set; }
public virtual Category category { get; set; }
public virtual Image image { get; set; } // Complex Type
public virtual User postedBy { get; set; }
public virtual ICollection<Score> scores { get; set; }
public virtual ICollection<Comment> comments { get; set; }
public Scab() {
scores = new List<Score>();
comments = new List<Comment>();
}
}
我也應該注意到,Image
屬性是一個ComplexType
在你的客戶端刪除''「scab」:{'屬性,這樣你的json數據應該是這樣的:'{ 「url」:... 「title」...}' – nemesv
就是這樣。你應該提交這個答案,以便我可以給你信用。謝謝 – bflemi3