如何将默认Web API 2更改为JSON格式化程序?

我有一个Web API项目,它返回一些产品数据。 它根据请求的Accept标头(JSON / XML)正确协商返回类型。 问题是,如果没有指定Accept标头,则返回XML,但我希望它默认返回JSON

http://website.com/MyPage?type=json // returns json http://website.com/MyPage?type=xml // returns xml http://website.com/MyPage // returns xml by default 

这是我目前的代码如下:

 GlobalConfiguration.Configuration.Formatters.XmlFormatter.MediaTypeMappings.Add( new QueryStringMapping("type", "xml", new MediaTypeHeaderValue("application/xml"))); GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add( new QueryStringMapping("type", "json", new MediaTypeHeaderValue("application/json"))); 

App_Start/WebApiConfig.cs添加:

 config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html")); 

我认为Web API只使用它可以在Formatters集合中找到的第一个格式化程序。 您可以使用类似的方式更改排序

 GlobalConfiguration.Configuration.Formatters.Clear(); GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter()); GlobalConfiguration.Configuration.Formatters.Add(new XmlMediaTypeFormatter()); 

但似乎默认情况下JSON格式化程序应该是第一个,因此您可能需要检查是否已在某处修改此集合。

我认为你应该改变如下。 Global.asax中:

 /*For Indented formatting:*/ GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented; /*Response as default json format * example (http://localhost:9090/WebApp/api/user/) */ GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear(); /*Response as json format depend on request type * http://localhost:9090/WebApp/api/user/?type=json */ GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add( new QueryStringMapping("type", "json", new MediaTypeHeaderValue("application/json"))); /*Response as xml format depend on request type * http://localhost:9090/WebApp/api/user/?type=xml */ GlobalConfiguration.Configuration.Formatters.XmlFormatter.MediaTypeMappings.Add( new QueryStringMapping("type", "xml", new MediaTypeHeaderValue("application/xml"))); 

或者只是删除XmlFormatter

 var formatters = GlobalConfiguration.Configuration.Formatters; formatters.Remove(formatters.XmlFormatter); 

上述答案都不适合我。 问题是我从GlobalConfiguration获取格式化程序,而不是使用new HttpConfiguration()创建的config对象。这是适用于我的代码:

 public class WebApiConfig { public static HttpConfiguration Register() { var config = new HttpConfiguration(); // This next line could stay if you want xml formatting config.Formatters.Remove(config.Formatters.XmlFormatter); // This next commented out line was causing the problem //var jsonFormatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter; // This next line was the solution var jsonFormatter = config.Formatters.JsonFormatter; jsonFormatter.UseDataContractJsonSerializer = false; // defaults to false, but no harm done jsonFormatter.SerializerSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat; jsonFormatter.SerializerSettings.Formatting = Formatting.None; jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); // remaining irrelevant code commented out return config; } } 
 config.EnableSystemDiagnosticsTracing(); GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear(); // Adding formatter for Json config.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("type", "json", new MediaTypeHeaderValue("application/json"))); // Adding formatter for XML config.Formatters.XmlFormatter.MediaTypeMappings.Add(new QueryStringMapping("type", "xml", new MediaTypeHeaderValue("application/xml")));