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

后端 未结 7 1707
攒了一身酷
攒了一身酷 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: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.

提交回复
热议问题