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

后端 未结 7 1676
攒了一身酷
攒了一身酷 2020-11-29 07:55

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 h

相关标签:
7条回答
  • 2020-11-29 08:25

    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

    0 讨论(0)
  • 2020-11-29 08:26

    Another solution in Asp.Net.Core 2.2 as following:

    services.AddMvc()
    .AddJsonOptions(jsonOptions => jsonOptions.UseMemberCasing())
    .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    
    0 讨论(0)
  • 2020-11-29 08:33

    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.

    0 讨论(0)
  • 2020-11-29 08:33

    I am using the following solution because

    • a) I prefer using the .Net Core built in System.Text.Json serializer and
    • b) I do not want to rely on the not documented internal behaviour of jsonOptions.JsonSerializerOptions.PropertyNamingPolicy = null;.

    .

    services.AddControllers()
        .AddJsonOptions(options =>
        {
            options.JsonSerializerOptions.PropertyNamingPolicy = new MyTransparentJsonNamingPolicy();
        });
    

    where:

    public class MyTransparentJsonNamingPolicy : JsonNamingPolicy
    {
        // You can came up any custom transformation here, so instead just transparently
        // pass through the original C# class property name, it is possible to explicit
        // convert to PascalCase, etc:
        public override string ConvertName(string name)
        {
            return name;
        }
    }
    
    0 讨论(0)
  • 2020-11-29 08:42

    You have to change the DefaultContractResolver which uses camelCase by default. Just set the NamingStatergy as null.

    This should be done in the StartUp.ConfirgureService as follows.

      public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc()
                .AddMvcOptions(o => o.OutputFormatters.Add(
                    new XmlDataContractSerializerOutputFormatter()));
    
                .AddJsonOptions(o => {
                    if (o.SerializerSettings.ContractResolver != null)
                    {
                        var castedResolver = o.SerializerSettings.ContractResolver
                            as DefaultContractResolver;
                        castedResolver.NamingStrategy = null;
                    }
                });
        }
    

    Option 2

    Use JSonProperty as follows.

    public class Hat
    {
        [JsonProperty("id")]
        public int Id { get; set; }
        [JsonProperty("name")]
        public string Name { get; set; }
        [JsonProperty("color")]
        public string Color { get; set; }
        [JsonProperty("count")]
        public int Count { get; set; }
    }
    
    0 讨论(0)
  • 2020-11-29 08:47

    For Asp.Net Core 3.1 using the NewtonSoft.Json

    services.AddControllers()
            .AddNewtonsoftJson(options =>
            {
                options.UseMemberCasing();
            });
    
    0 讨论(0)
提交回复
热议问题