Formatting JSON output in CamelCase versus the default .NET's PascalCase

by Admin 24. February 2017 07:27

Add to your WebApiConfig.cs file (under App_Start folder at WebApi 2) the following two lines:

// Get the default json formatter
var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();

// Switch from PascalCase to CamelCase
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

 

.NET Core

public void ConfigureServices(IServiceCollection services)

        {

            services.AddMvc()

                    .AddJsonOptions(options =>

                    {

                        options.SerializerSettings.ContractResolver =

                            new CamelCasePropertyNamesContractResolver();

                    });

        }

Tags:

Comments are closed

TextBox

Tag cloud

    RecentPosts