0

I am attempting to force a camelCase response with the following line in my ConfigureApp method:

        config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

The entire method:

        public static void ConfigureApp(IAppBuilder appBuilder)
        {
            // Configure Web API for self-host. 
            HttpConfiguration config = new HttpConfiguration();
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.JsonFormatter.UseDataContractJsonSerializer = false;
            appBuilder.UseWebApi(config);
        }

And I'm returning the following response from my controller:

    public object Get()
    {
        var result = new { name = "alex", number = 1, lastname = "smith" };

        return Ok(result);
    }

The response I am getting is:

{"name":"alex","number":1,"lastname":"smith"}

The response I am expecting is:

{"name":"alex","number":1,"lastName":"smith"}

How do I force camelCase response (lastname vs lastName)?

Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
  • 1
    This is not a duplicate, as the configuration is exactly inline with the suggested approach here: http://stackoverflow.com/questions/28552567/web-api-2-how-to-return-json-with-camelcased-property-names-on-objects-and-the – Alex Gordon May 16 '17 at 17:50
  • 2
    @MeggieLuski if you want lastname to be lastName, then in c# your property name should be LastName not Lastname. – johnny 5 May 16 '17 at 18:00

0 Answers0