讓WEBAPI始終返回JSON格式數據

方法一

WEB API默認支持返回XML和JSON 格式數據,其根據WEB請求中指定的類型自動識別,但在本項目中只需要用json格式,而且爲了在chrome中測試方便(用chrome瀏覽器默認返回是xml數據),限制其無論什麼情況都返回JSON格式。

Application_Start中加上:

GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();

方法二

上法或者太暴力了,來個溫和點的

Application_Start中加上:

GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(new
QueryStringMapping(“format”, “json”, “application/json”));


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章