Web API Camel Case Contract Resolver Reference

February 06, 2016

As you start a new ASP.NET WEB project that has a RESTful service, you may find yourself Googling for the 3 lines that convert your JSON data from proper cased property names to camel cased, the way that JavaScript developers are used to. I've googled it and put together this quick snippet for you to reference as you continue your project!

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Owin;
using Owin;
using System.Web.Http;
using Newtonsoft.Json.Serialization;

[assembly: OwinStartup(typeof(ProjetName.API.Startup))]

namespace ProjectName.API
{
    public partial class Startup
    {
		public Startup()
		{
		}
                public void Configuration(IAppBuilder app)
                {
			HttpConfiguration config = GlobalConfiguration.Configuration;
			config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
			config.Formatters.JsonFormatter.UseDataContractJsonSerializer = false;
		}
    }
}

Share

Comments