1

I'm trying to configure my website so every json is serialized in camelCase, which is not the case right now.

I've read several posts in which everyone suggests that I need to configure the Global.asax file, and so far I've tried several ways and none of them where succesful:

protected void Application_Start()
    {
         JsonConvert.DefaultSettings = () =>
        {
            var settings = new JsonSerializerSettings();
            settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            return settings;
        };

        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

protected void Application_Start()
    {

        GlobalConfiguration.Configuration
                            .Formatters
                            .JsonFormatter
                            .SerializerSettings
                            .ContractResolver = new CamelCasePropertyNamesContractResolver();


        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }


protected void Application_Start()
    {
        HttpConfiguration config = GlobalConfiguration.Configuration;
        config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        config.Formatters.JsonFormatter.UseDataContractJsonSerializer = false;

        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

protected void Application_Start()
    {

        var jsonSerializer = Newtonsoft.Json.JsonSerializer.Create(new JsonSerializerSettings
        {
            ContractResolver = new CamelCasePropertyNamesContractResolver()
        });


        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

I'm pretty sure I'm doing something wrong, I just don't know what it is.

I don't really need to do this application wise, configuring the specific controller would also work.

Some information about the proyect: this is a MVC5 application, running in .net framework 4.7.2

Thanks a lot!

Rambo3
  • 381
  • 1
  • 4
  • 18

0 Answers0