Dealing with Camel Casing in ASP.NET Core Web API

Some time ago I wrote a three part article on creating and consuming Web API in ASP.NET Core. You can read them here - Part 1, Part 2, Part 3.

If you worked with that example you must have noticed that while serializing data to the client, the ASP.NET Core Web API uses camel casing. That means if your server side C# class is like this :

public class Employee
    public int EmployeeID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string City { get; set; }

then post JSON serialization a sample Employee object will look like this :


Firefox nicely shows the output as shown in the following figure.

As you can see all the property names have been converted to their camel case equivalents (EmployeeID to employeeID, FirstName to firstName and so on).

This default behavior doesn't post much problems if the client is a C# application (HttpClient based) but if the client is a JavaScript client you might need to alter this behavior. Even with JavaScript clients the camel casing may not pose any problem in many cases. That's because camel casing is heavily used in JavaScript world. Especially if you are using some JS framework chances are you will be using camel casing during data binding and similar things.

At times, however, you may want to preserve the casing of the original C# property names. Suppose you are migrating or reusing an existing piece of JavaScript code that uses the same casing as the C# class. So, you might want to prevent that code from breaking. This requires that the JSON serialization of ASP.NET Core preserve the casing of the underlying C# class.

Although the default behavior is to use camel casing, you can easily change that to preserve the original casing. Let's see how.

Open the Startup class of the Web API application and go to the ConfigureServices() method. Currently you have the following call there:


Change that line to this:

        .AddJsonOptions(options => 
         = new DefaultContractResolver());

The above code uses AddJsonOptions() extension method. The AddJsonOptions() method then specifies the ContractResolver property to an instance of DefaultContractResolver class. Note that for the above code to compile correctly you need to do the following:

  • Import Newtonsoft.Json.Serialization namespace
  • Add NuGet package for Microsoft.AspNetCore.Mvc.Formatters.Json (see below)


If you run the application now, you should see the JSON data as shown below.

As you can see, now the camel casing has been removed. Remember that DefaultContractResolver preserves whatever is the casing of the C# class. It doesn't automatically convert it to pascal casing. Just to make this clear have a look at the following figure.

Observe the casing of the properties. The underlying C# class has been modified to use the casing as shown above. The same casing is preserved during JSON serialization.

What if you need to explicitly specify that you want camel casing? Simple. Just use CamelCasePropertyNamesContractResolver class. The following code shows how.

        .AddJsonOptions(options => 
        = new CamelCasePropertyNamesContractResolver());

Now we set the ContractResolver to a new instance of CamelCasePropertyNamesContractResolver class. This will use camel casing during JSON serialization.

That's it for now! Keep coding!!

Bipin Joshi is an independent software consultant, trainer, author, and meditation teacher. He has been programming, meditating, and teaching for 25+ years. He conducts instructor-led online training courses in ASP.NET family of technologies for individuals and small groups. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Ajapa Yoga to interested individuals. To know more about him click here.

Get connected : Facebook  Twitter  LinkedIn  YouTube

Posted On : 27 June 2017