Created
April 24, 2016 02:47
-
-
Save seangwright/85edbb18eb1433d24c7d1d9e37c70ded to your computer and use it in GitHub Desktop.
WebApiConfig.cs
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
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