Created
April 15, 2015 21:12
-
-
Save michaeljbailey/8e6fd69711b4427dbeea to your computer and use it in GitHub Desktop.
Sane defaults to use for pure JSON Web APIs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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