There is a general agreement to use PascalCasing for your objects in .NET, this in contrast to JavaScript where camelCasing is the default. This is a little bit unfortunate when you are using ASP.NET Web API and want to return a JSON object to your JavaScript object. By default the properties will be PascalCased because of the fact that your C# objects are PascalCased as well.
Luckily this is an easy one to fix using the Json.NET serializer. Inside the WebAPIConfig class, add the following lines:
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 WebApiConfig | |
{ | |
public static void Register(HttpConfiguration config) | |
{ | |
// Web API configuration and services | |
// Web API routes | |
config.MapHttpAttributeRoutes(); | |
//config.Routes.MapHttpRoute( | |
// name: "DefaultApi", | |
// routeTemplate: "api/{controller}/{id}", | |
// defaults: new { id = RouteParameter.Optional } | |
//); | |
var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First(); | |
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); | |
} | |
} | |