A little experimentation seems to indicate that the order of the configured formats matters (which is pretty intuitive).
By default, when creating an instance, HttpConfigurationits collection Formatterscontains the following formats:
- XmlMediaTypeFormatter
- JsonValueMediaTypeFormatter
- JsonMediaTypeFormatter
- FormUrlEncodedMediaTypeFormatter
The reason XML is the default formatting is because it is the first formatting. To make JSON the default, you can reorder the collection this way:
- JsonValueMediaTypeFormatter
- JsonMediaTypeFormatter
- XmlMediaTypeFormatter
- FormUrlEncodedMediaTypeFormatter
Given an instance of configHttpConfiguration, here is one way to change the collection order:
var jsonIndex = Math.Max(
config.Formatters.IndexOf(config.Formatters.JsonFormatter),
config.Formatters.IndexOf(config.Formatters.JsonValueFormatter));
var xmlIndex = config.Formatters.IndexOf(
config.Formatters.XmlFormatter);
config.Formatters.Insert(jsonIndex + 1, config.Formatters.XmlFormatter);
config.Formatters.RemoveAt(xmlIndex);
Whether this is supported or not, I don’t know, but it seems to work on WebApi 0.6.0.