问题
I have a Web API project that returns some product data. It negotiates the return type correctly depending on the Accept header (JSON/XML) of the request. The problem is, if no Accept header is specified it returns XML, but I want it to return JSON by default
http://website.com/MyPage?type=json // returns json
http://website.com/MyPage?type=xml // returns xml
http://website.com/MyPage // returns xml by default
Here is my current code looks like:
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")));
回答1:
Add this in your App_Start/WebApiConfig.cs
:
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
回答2:
I think Web API just uses the first formatter it can find in the Formatters collection. You can change the ordering with something like
GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter());
GlobalConfiguration.Configuration.Formatters.Add(new XmlMediaTypeFormatter());
But it seems the JSON formatter should be the first one by default so you might want to check if you're already modifying this collection somewhere.
回答3:
I think you should change as following. 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")));
回答4:
Or just remove the XmlFormatter
var formatters = GlobalConfiguration.Configuration.Formatters;
formatters.Remove(formatters.XmlFormatter);
回答5:
None of the above answers worked for me. The problem was that I was getting hold of the formatters from GlobalConfiguration
and not the config
object created with new HttpConfiguration()
Here is the code that works for me :
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;
}
}
回答6:
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")));
来源:https://stackoverflow.com/questions/25224824/how-to-change-default-web-api-2-to-json-formatter