How to turn off or handle camelCasing in JSON response ASP.NET Core?

I’m running through a WintellectNOW course on ASP.NET Core/Web API/Angular 2. I have the API portion implemented, but for whatever reason, the JSON that is being returned has the variable names being lowercased.

The returned JSON is formatted like…

 {"id":3,"name":"Baseball Cap","color":"blue","count":3}

I’m expecting…

 {"Id":3,"Name":"Baseball Cap","Color":"blue","Count":3}

Based on the C# model of…

namespace HatCollection.Models
    public class Hat
        public int Id { get; set; }
        public string Name { get; set; }
        public string Color { get; set; }
        public int Count { get; set; }

I even went as far as decorating the properties with [DataMember(Name = "Id")] just to make sure and it still didn’t matter.

On the off chance, it’s relevant the Action and instance variable in the controller…

private static readonly List<Hat> MyHats = new List<Hat>
    new Hat {Id = 1, Name = "Bowler", Color = "black", Count = 1 },
    new Hat {Id = 2, Name = "Fedora", Color = "red", Count = 1 },
    new Hat {Id = 3, Name = "Baseball Cap", Color = "blue", Count = 3 }

public IEnumerable<Hat> Get()
    return MyHats;

How do I turn off the camelCase functionality, so that ASP.NET Core returns the property names without changing them?


In ASP.NET Core <3.0, JSON properties are camelCased by default (per this announcement).

You can disable this by replacing



    .AddJsonOptions(opt => opt.SerializerSettings.ContractResolver
        = new DefaultContractResolver());

in your Startup.cs file. You’ll have to add using Newtonsoft.Json.Serialization; to the top of the file.

With the DefaultContractResolver in place, the property names will be represented verbatim in the JSON output. No need for DataMember attributes.

Leave a Reply

Your email address will not be published. Required fields are marked *