Meditation and Mindfulness for Software / IT Professionals. Conducted by Bipin Joshi in Thane. Read more...

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 :

[Table("Employees")]
public class Employee
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Required]
    public int EmployeeID { get; set; }
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }
    [Required]
    public string City { get; set; }
}

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

{
 "employeeID":1,
 "firstName":"Nancy",
 "lastName":"Davolio",
 "city":"Seattle"
}

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:

services.AddMvc();

Change that line to this:

services.AddMvc()
        .AddJsonOptions(options => 
        options.SerializerSettings.ContractResolver
         = 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.

services.AddMvc()
        .AddJsonOptions(options => 
        options.SerializerSettings.ContractResolver 
        = 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 a software consultant, an author and a yoga mentor having 21+ years of experience in software development. He conducts online courses in ASP.NET MVC / Core, jQuery, and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced Yoga way of life he also teaches Meditation to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 27 Jun 2017



Tags : ASP.NET ASP.NET Core MVC C#