Keep Web API JSON Object names same as Class property - ASP.NET Core

.NET Core by default, converts the class property names to Camel case when we serialize the JSON object.
e.g. I have defined my class as below









when we serialize this class, the web API will return as following

So in order to make it as the property names as same as the class property name like below







we need to override the ContractResolver in JSON serialization settings.
services
    .AddMvc()
    .AddJsonOptions(options =>
    {
        options.SerializerSettings.ContractResolver
            = new Newtonsoft.Json.Serialization.DefaultContractResolver();
    });
Note: AddJsonOptions method will be available in Microsoft.Extensions.DependencyInjection.MvcJsonMvcBuilderExtensions class

Gopikrishna

    Blogger Comment
    Facebook Comment

1 comments:

  1. Really helpful. Please post some information above AWS.

    ReplyDelete