How to return JSON result in Camel-case format from a ASP.NET MVC Controller?

I’m working on a AngularJS application where this client application is a part of ASP.NET MVC application. So, the angularjs web app talks to the controller which is already there to be consumed. All the controllers return JSON result to be consumed by any client which is in Pascal-case format (e.g. IsValid or FullName). However, to consume this result in my angularjs application, I’d like to get result in Camel-case format as all  of my javascript definition follows the same format. So, in this article, I’ll explain a way to return JSON result in Camel-case format from a ASP.NET MVC Controller (e.g.: isValid or fullName).

My web application uses Json.NET – a JSON framework for .NET.  I’ve a base MVC controller that overrides the JSON result method that can be used by any controllers that derives from this base controller, thus, making the Camel-case JSON result as reusable. The code for the base controller is below:

The implementation of the JSON override returns a result of type JsonDotNetResult that is a custom type of ActionResult that derives from built-in JsonResult. The code of this custom type is below:

The ContractResolver is the main setting where we instruct JSON.NET that use the camel-case contract resolver when serialising an object and then it’s added to the response object which is then sent to the client/browser.

Usage of the above code is below where a controller derives from the base controller that is setup to use JSON result that returns an object in camel-case format.

Hope this helps, if you face any issues, comment here and I’ll try to help.

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