Skip to content

Instantly share code, notes, and snippets.

@michaeljbailey
Created April 15, 2015 21:12
Show Gist options
  • Save michaeljbailey/8e6fd69711b4427dbeea to your computer and use it in GitHub Desktop.
Save michaeljbailey/8e6fd69711b4427dbeea to your computer and use it in GitHub Desktop.
Sane defaults to use for pure JSON Web APIs
public static class FormatConfig
{
private class JsonContentNegotiator : IContentNegotiator
{
private readonly JsonMediaTypeFormatter _jsonFormatter;
public JsonContentNegotiator(JsonMediaTypeFormatter formatter)
{
_jsonFormatter = formatter;
}
public ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters)
{
return new ContentNegotiationResult(_jsonFormatter, new MediaTypeHeaderValue("application/json"));
}
}
public static void Configure(HttpConfiguration configuration)
{
configuration.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(new JsonMediaTypeFormatter
{
Indent = false,
UseDataContractJsonSerializer = true,
SerializerSettings = new JsonSerializerSettings
{
ConstructorHandling = ConstructorHandling.Default,
DateFormatHandling = DateFormatHandling.IsoDateFormat,
DateParseHandling = DateParseHandling.DateTime,
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
Formatting = Formatting.None,
}
}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment