Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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":1,"name":"Bowler","color":"black","count":1},
 {"id":2,"name":"Fedora","color":"red","count":1},
 {"id":3,"name":"Baseball Cap","color":"blue","count":3}
]

I'm expecting...

[
 {"Id":1,"Name":"Bowler","Color":"black","Count":1},
 {"Id":2,"Name":"Fedora","Color":"red","Count":1},
 {"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 }
};

[HttpGet]
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?

like image 898
Jared Avatar asked Aug 02 '16 18:08

Jared


4 Answers

In Asp.Net Core 3.0 some things have changed. For camelCase do nothing that is out of the box. For PascalCase or another set style use.

services.AddMvc(setupAction=> {
            setupAction.EnableEndpointRouting = false;
        }).AddJsonOptions(jsonOptions =>
        {
            jsonOptions.JsonSerializerOptions.PropertyNamingPolicy = null;
        })
        .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

In Startup.cs ConfigureServices section

like image 197
Martijn van Halen Avatar answered Nov 16 '22 09:11

Martijn van Halen


For those who needs a solution about a PascalCase within Api Project that has not the Mvc services you should add this after AddControllers services

 // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers().AddJsonOptions(jsonOptions =>
                {
                    jsonOptions.JsonSerializerOptions.PropertyNamingPolicy = null;
                } ;
        }
like image 34
BRAHIM Kamel Avatar answered Nov 16 '22 09:11

BRAHIM Kamel


For Asp.Net Core 3.1 using the NewtonSoft.Json

services.AddControllers()
        .AddNewtonsoftJson(options =>
        {
            options.UseMemberCasing();
        });
like image 40
pantonis Avatar answered Nov 16 '22 09:11

pantonis


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

You can disable this by replacing

services.AddMvc();

with

services
    .AddMvc()
    .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.

like image 26
Nate Barbettini Avatar answered Nov 16 '22 08:11

Nate Barbettini