Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class WebApiConfig
- {
- public static void Register(HttpConfiguration config)
- {
- // Web API configuration and services
- var json = config.Formatters.JsonFormatter;
- json.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
- var jsonFormatter = config.Formatters.JsonFormatter;
- var jSettings = new JsonSerializerSettings
- {
- Formatting = Formatting.Indented,
- DateTimeZoneHandling = DateTimeZoneHandling.Utc
- };
- jSettings.Converters.Add(new DateTimeConvertor());
- jsonFormatter.SerializerSettings = jSettings;
- config.Formatters.Remove(config.Formatters.XmlFormatter);
- config.Filters.Add(new BasicAuthenticatorFilter("Mondino"));
- config.MapHttpAttributeRoutes();
- config.Routes.MapHttpRoute(
- name: "DefaultApi",
- routeTemplate: "api/{controller}/{id}",
- defaults: new { id = RouteParameter.Optional }
- );
- }
- }
- public class DateTimeConvertor : DateTimeConverterBase
- {
- public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
- {
- return DateTime.Parse(reader.Value.ToString());
- }
- public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
- {
- writer.WriteValue(((DateTime)value).ToIsoDate());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment