Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Force PascalCase Json Format #1745


User avatar
0
bagy88 created

Hello :)

I noticed that the json Format in ASP.NET Boilerplate is camelCase. How to force it to be PascalCase? Do i have to write my Formatter or?

Tnx :)


5 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    A solution is mentioned here <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/issues/769">https://github.com/aspnetboilerplate/as ... issues/769</a>. You can also change ContractResolver in your WebApiModule's initialize method like this.

    Configuration.Modules.AbpWebApi().HttpConfiguration.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new DefaultContractResolver();
    
  • User Avatar
    0
    lcyhjx created

    Hi ismcagdas,,

    I add following following code

    Configuration.Modules.AbpWebApi().HttpConfiguration.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new DefaultContractResolver();
    

    in WebApiModel.Initialize, and the return response still camelCase, not pascalCase.

    Then I write a customerContractResolver for pascalCase, still not work

    public class PascalCasePropertyNamesContractResolver : DefaultContractResolver
            {
                public new static readonly PascalCasePropertyNamesContractResolver Instance = new PascalCasePropertyNamesContractResolver();
    
                protected override string ResolvePropertyName(string propertyName)
                {
                    return propertyName.ToPascalCase();           
                }
            }
    
    Configuration.Modules.AbpWebApi().HttpConfiguration.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new PascalCasePropertyNamesContractResolver();
    

    Do you know What am i missing?

    BTW, my abp version is 1.2.0.0

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you try to move it to PostInitialize.

  • User Avatar
    0
    lcyhjx created

    Thanks so much! Move following code to PostInitialize, now it works fine. Configuration.Modules.AbpWebApi().HttpConfiguration.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new DefaultContractResolver();

  • User Avatar
    0
    ismcagdas created
    Support Team

    Great :)