我如何获得的ASP.NET Web API使用Chrome返回JSON,而不是XML? [英] How do I get ASP.NET Web API to return JSON instead of XML using Chrome?
问题描述
使用较新的的ASP.NET Web API 在 Chrome浏览器我看到XML - 我怎样才能改变它要求 JSON 让我可以在浏览器中查看它?我相信这只是请求头的一部分,我在正确的吗?
Using the newer ASP.NET Web API, in Chrome I am seeing XML - how can I change it to request JSON so I can view it in the browser? I do believe it is just part of the request headers, am I correct in that?
推荐答案
我刚刚加入App_Start / WebApiConfig.cs级以下在我的MVC的Web API项目。
I just add the following in App_Start/WebApiConfig.cs class in my MVC Web API project.
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html") );
这可以确保得到大部分的查询JSON,但是当你发送文字/ XML,你可以得到XML。
That makes sure you get json on most queries, but you can get xml when you send text/xml.
如果您需要有响应的Content-Type为application / JSON请检查以下 托德的回答。
If you need to have the response Content-Type as application/json please check Todd's answer below.
这篇关于我如何获得的ASP.NET Web API使用Chrome返回JSON,而不是XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!