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
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")));
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")));
Or just remove the XmlFormatter
var formatters = GlobalConfiguration.Configuration.Formatters;
formatters.Remove(formatters.XmlFormatter);
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;
}
}
Add this in your App_Start/WebApiConfig.cs
:
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
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.