我有一个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")));
答案 0 :(得分:23)
在string Sampler::Serial() const
:
App_Start/WebApiConfig.cs
答案 1 :(得分:14)
我认为Web API只使用它可以在Formatters集合中找到的第一个格式化程序。您可以使用
之类的内容更改顺序GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter());
GlobalConfiguration.Configuration.Formatters.Add(new XmlMediaTypeFormatter());
但是JSON格式化程序似乎应该是默认情况下的第一个格式化程序,因此您可能需要检查是否已在某处修改此集合。
答案 2 :(得分:9)
我认为你应该改变如下。 的的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")));
答案 3 :(得分:5)
或者只是删除XmlFormatter
var formatters = GlobalConfiguration.Configuration.Formatters;
formatters.Remove(formatters.XmlFormatter);
答案 4 :(得分:3)
上述答案都不适合我。问题是我从GlobalConfiguration
获取了格式化程序而不是config
创建的new HttpConfiguration()
对象。以下是适用于我的代码:
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;
}
}
答案 5 :(得分:1)
FYI注意拦截text/html
媒体类型,因为这也会格式化来自服务器的404响应。就我而言,这导致了潜在的安全问题,因为:
window.location
或任何险恶的东西答案 6 :(得分:0)
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")));