從this位置下載了WCF REST模板。WCF 4.0:WebMessageFormat.Json不能與WCF REST模板一起使用
默認的響應格式是XML,這很好用。但是,當我嘗試獲得JSON響應時,我仍然獲得XML。
這是我修改後的代碼 -
[WebGet(UriTemplate = "",ResponseFormat = WebMessageFormat.Json)]
public List<SampleItem> GetCollection()
{
// TODO: Replace the current implementation to return a collection of SampleItem instances
return new List<SampleItem>() { new SampleItem() { Id = 1, StringValue = "Hello" } };
}
注意ResponseFormat = WebMessageFormat.Json。這是我對該模板做的唯一改變。
我錯過了什麼?
謝謝!
我正在使用.NET Framework 3.5,如何做到這一點呢? – 2011-05-17 12:53:35
+1有趣的是,我在IE中的silverlight應用會得到json,而在firefox 4中運行的同一個應用會得到xml。這固定它。 – 2011-06-07 15:13:37
+1令人難以置信的ResponseFormat = WebMessageFormat.Json如何被默默地忽略,你必須弄清楚這一點!如果沒有谷歌,WCF將完全無法使用 – Andomar 2011-07-25 11:30:30