Skip to content

Instantly share code, notes, and snippets.

@seangwright
Created April 24, 2016 02:47
Show Gist options
  • Save seangwright/85edbb18eb1433d24c7d1d9e37c70ded to your computer and use it in GitHub Desktop.
Save seangwright/85edbb18eb1433d24c7d1d9e37c70ded to your computer and use it in GitHub Desktop.
WebApiConfig.cs
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Formatting;
using System.Web.Http;
using WiredViews.WIR03.Web.Api.Exceptions;
namespace WiredViews.WIR03.Web.Api.Configuration
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config, string baseApiRoute)
{
// Only for local debugging
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.LocalOnly;
// Web API routes
config.MapHttpAttributeRoutes(new BasePrefixRouteProvider(baseApiRoute));
// Remove XML formatter and any others
config.Formatters.Clear();
JsonMediaTypeFormatter formatter = new JsonMediaTypeFormatter();
JsonSerializerSettings settings = formatter.SerializerSettings;
settings.Formatting = Formatting.Indented;
// UTC Date serialization configuration
settings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
settings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
settings.DateFormatString = "yyyy-MM-ddTHH:mm:ss.fffK";
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
// Add Json serializer with CamelCase resolver
config.Formatters.Add(formatter);
config.Services.Replace(typeof(System.Web.Http.ExceptionHandling.IExceptionHandler), new GlobalExceptionHandler());
// Ensure Web Api is configured correctly (routes, formatting, ect...)
config.EnsureInitialized();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment