JSON properties now lower case on swap from ASP .Net Core 1.0.0-rc2-final to 1.0.0
Asked Answered
V

13

140

I've just swapped our project from ASP .Net Core 1.0.0-rc2-final to 1.0.0. Our website and client have stopped working because of the capitalization of JSON properties. For example, this line of JavaScript now fails

for (var i = 0; i < collection.Items.length; i++){

because the controller now calls the array "items" instead of "Items". I have made no changes beyond installing the updated packages and editing the project.json file. I have not changed the C# model files which still capitalize their properties.

Why have the ASP.Net Core controllers started returning JSON with lower-cased properties? How do I go back to them honoring the case of the property names from the model?

Vestibule answered 5/7, 2016 at 11:15 Comment(2)
see github.com/aspnet/Announcements/issues/194Contralto
For those of you looking for the answer for .Net Core 3 you need to scroll down to the bottom of the page.Zoomorphism
C
182

MVC now serializes JSON with camel case names by default

Use this code to avoid camel case names by default

  services.AddMvc()
        .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());

Source: https://github.com/aspnet/Announcements/issues/194

Civics answered 5/7, 2016 at 11:41 Comment(2)
netcore2: using Newtonsoft.Json.Serialization;Endanger
I don't want to change all of my JSON serialization, just one particular case that needs to match the behaviour of some third-party software. How does one provide the correct serialization settings in the second parameter of the Json() call?Stood
D
197

In case you found this from Google and looking for a solution for Core 3.

Core 3 uses System.Text.Json, which by default does not preserve the case. As mentioned with this GitHub issue, setting the PropertyNamingPolicy to null will fix the problem.

public void ConfigureServices(IServiceCollection services)
{
...
    services.AddControllers()
            .AddJsonOptions(opts => opts.JsonSerializerOptions.PropertyNamingPolicy = null);

and if you don't want to change the global settings, for one action only it's like this:

return Json(obj, new JsonSerializerOptions { PropertyNamingPolicy = null });
Druse answered 1/10, 2019 at 15:13 Comment(4)
you are my lord and saviorKeratosis
This worked well for me on Core 3.1. I couldn't get my kendo components to work properly and found the json to be camel case formatted. This resolved the issue.Philipphilipa
I'm also your subjectTeakettle
This saved my live!Sclerometer
C
182

MVC now serializes JSON with camel case names by default

Use this code to avoid camel case names by default

  services.AddMvc()
        .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());

Source: https://github.com/aspnet/Announcements/issues/194

Civics answered 5/7, 2016 at 11:41 Comment(2)
netcore2: using Newtonsoft.Json.Serialization;Endanger
I don't want to change all of my JSON serialization, just one particular case that needs to match the behaviour of some third-party software. How does one provide the correct serialization settings in the second parameter of the Json() call?Stood
C
19

You can change the behavior like this:

services
    .AddMvc()
    .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());

See the announcement here: https://github.com/aspnet/Announcements/issues/194

Contralto answered 5/7, 2016 at 11:19 Comment(0)
I
19

For those who migrated to Core 3.1 and have Core MVC project can use following setup code in Startup.cs:


        public void ConfigureServices(IServiceCollection services)
        {
            ...
            services.AddControllersWithViews().AddJsonOptions(opts => opts.JsonSerializerOptions.PropertyNamingPolicy = null);
            ...
        }
Infighting answered 14/1, 2020 at 20:14 Comment(2)
This is the same as a prior answer https://mcmap.net/q/165540/-json-properties-now-lower-case-on-swap-from-asp-net-core-1-0-0-rc2-final-to-1-0-0Gooseherd
@MarkSchultheiss you are probably right, as my answer is similar to all the other ones, however, when I was searching proper answer based on my project type, which is MVC utilizing views, I didn't find exact answer matching my project. AddControllersWithViews() adds those missing piece, and I thought it will be useful for anyone in the future. Thank you though for your comment!Infighting
M
7

This will fix it in dotnet core 3 webapi, so that it doesn't change your property names at all, and you return to your client exactly what you intended to.

In Startup.cs:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers().AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null);
        services.AddHttpClient();
    }
Makings answered 31/10, 2019 at 15:2 Comment(1)
M
5

For someone who does not want to set it globally, it is possible to use ContractResolver also to return as Json result:

public IActionResult MyMethod()
{
    var obj = new {myValue = 1};
    return Json(obj, new JsonSerializerSettings {ContractResolver = new DefaultContractResolver()});
}
Microtone answered 3/12, 2020 at 9:36 Comment(0)
A
3

For ASP MVC Core 6 Web API , Add below code into Program.cs file will make sure JSON propreties name follow C# model properties name in right casing on swagger UI. No 3rd party package require

using Microsoft.AspNetCore.Mvc; 


builder.Services.Configure<JsonOptions>(options =>
{
    options.JsonSerializerOptions.PropertyNamingPolicy = null;
});
Ace answered 16/2, 2023 at 9:29 Comment(1)
This solution worked as well for .Net Core 7. Thanks a lotHendrickson
K
2

Install

Microsoft.AspNetCore.Mvc.NewtonsoftJson

For .net 6 you should select this version: 6.0.13

and then go to Program.cs and configure it like this

builder.Services
.AddControllersWithViews()
.AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
Khudari answered 10/1, 2023 at 23:51 Comment(1)
this answer helps me a lotSemirigid
E
1

For some one who is using ASP.net WEB API ( rather than ASP.NET Core).

Add this line in your WebApiConfig.

//Comment this jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

jsonFormatter.SerializerSettings.ContractResolver = new DefaultContractResolver();

Adding this as an answer here because this comes up first in google search for web api as well.

Expressman answered 16/10, 2018 at 8:16 Comment(0)
T
0

For Core 2.x versions, using this code you can avoid camel case names by default. You need to add following code inside the ConfigureServices method of Startup.cs file.

services.AddMvc()
.AddJsonOptions(o =>
{
    if (o.SerializerSettings.ContractResolver != null)
    {
        var castedResolver = o.SerializerSettings.ContractResolver
        as DefaultContractResolver;

        castedResolver.NamingStrategy = null;
    }
});
Teter answered 13/1, 2020 at 16:2 Comment(0)
B
0

Recently had this issue with .Net6

The solution turned out to be that I needed to install

Microsoft.AspNetCore.Mvc.NewtonsoftJson 6.0.0.x (Note, use 7.x for .Net 7)

Found this out from Mason's post:

Microsoft.AspNetCore.Mvc.NewtonsoftJson 6.0.2 is not compatible with net5.0

Boondoggle answered 14/11, 2022 at 20:51 Comment(0)
A
0

Here's how to solve this using .NET 6 Minimal API:

builder.Services.Configure<Microsoft.AspNetCore.Http.Json.JsonOptions>(options =>
{
    options.SerializerOptions.PropertyNameCaseInsensitive = false;
    options.SerializerOptions.PropertyNamingPolicy = null;
    options.SerializerOptions.WriteIndented = true;
});

Solution from here.

Analogical answered 28/10, 2023 at 8:6 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Preposition
I
-1
using Microsoft.AspNetCore.Mvc; 

builder.Services.Configure<JsonOptions>(options =>
{
    options.JsonSerializerOptions.PropertyNamingPolicy = null;
});
Im answered 6/7, 2023 at 4:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.