How to return JSON result in camel-case format from ASP.NET Web API 2 Controller?

ASP.NET Web API V1 & V2 both return response in pascal-case format out of the box. Developers would like to change this if they would like to support mobile or JavaScript clients. The goal of this post is to explain how to return JSON result in camel-case format from  ASP.NET Web API 2 controller.

I’ve got a ASP.NET Web API V2 application opened in Visual Studio 2015. Similar setting is also possible for Web API V1. If you are interested to find out similar concept for ASP.NET MVC Controllers that you can read my another post.

Following is the WebApiConfig class in WebApiConfig.cs file:

Let’s understand the code highlighted above:

  • On line no. 8, we get the serializer settings of JSON formatter as this is where we would like to instruct for customization.
  • Adding indentation formatting is optional that is shown on line no. 9.
  • The important statement is on line no. 10, that sets the serializer settings resolver to CamelCasePropertyNamesContractResolver. This resolver basically resolves member mappings for a type, camel casing property names. This ContractResolver is then used by the serializer when serializing .NET objects to JSON and vice versa.

So, just to illustrate, the output of a GET HTTP action for a resource named Product will look like this:

Hope this post helps you to learn how to return JSON result in camel-case format from ASP.NET Web API 2 controller.

Siddharth Pandey

Siddharth Pandey is a Software Engineer with thorough hands-on commercial experience & exposure to building enterprise applications using Agile methodologies. Siddharth specializes in building, managing on-premise, cloud based real-time standard, single page web applications (SPAs). He has successfully delivered applications in health-care, finance, insurance, e-commerce sectors for major brands in the UK. Other than programming, he also has experience of managing teams, trainer, actively contributing to the IT community by sharing his knowledge using Stack Overflow, personal website & video tutorials.

You may also like...

Advertisment ad adsense adlogger