asp.net core 1.0 web api use camelcase

23,954

Solution 1

services
    .AddMvc()
    .AddJsonOptions(options =>
    {
        options.SerializerSettings.ContractResolver
            = new Newtonsoft.Json.Serialization.DefaultContractResolver();
    });

This keeps a JSON object's name the same as .NET class property.

Solution 2

You can configure JSON behavior this way:

public void ConfigureServices(IServiceCollection services)  
  {
      services.AddMvc()
                  .AddJsonOptions(options =>
                  {
                      options.SerializerSettings.ContractResolver =
                          new CamelCasePropertyNamesContractResolver();
                  });
  }
Share:
23,954
Brivvirs
Author by

Brivvirs

By day: Full stack asp.net developer By night: ZZZzzzZZzzz...

Updated on May 01, 2020

Comments

  • Brivvirs
    Brivvirs about 4 years

    On RC2 the same code returns json format with camel case. After netcore 1.0 release i started new project and the same code is returning json in lowercase.

    Tried multiple solutions but none of them were working web-api-serialize-properties-starting-from-lowercase-letter

  • Omu
    Omu almost 8 years
    this actually now is the default behavior (unfortunately), he wanted Default, as is, no change in property names, case
  • Kyle Gobel
    Kyle Gobel over 7 years
    What namespace/references does this require. There is no 'AddJsonOptions' that I'm able to see
  • Brivvirs
    Brivvirs over 7 years
    Its MVC6.namespace Microsoft.Extensions.DependencyInjection // Extensions methods for configuring MVC via an Microsoft.Extensions.DependencyInjection.IMvcBuilder. class MvcJsonMvcBuilderExtensions
  • JMK
    JMK over 6 years
    @Omu These actually aren't the same (in .net core 2.0 anyway). The DefaultContractResolver will accept CamelCase and return TitleCase, the CamelCasePropertyNamesContractResolver accepts and returns CamelCase.
  • Hinrich
    Hinrich over 6 years
    @Omu has this changed from .net core 1.x to 2.0 ?
  • Floxy
    Floxy about 6 years
    thanks !! i have the same Problem this one work fine
  • ibrahimozgon
    ibrahimozgon over 5 years
    Works fine. Thanks.