.net Framework 4.7.2 json设置

时间:2018-09-25 17:35:32

标签: .net json asp.net-mvc

我正在尝试配置我的网站,以便每个json都在camelCase中序列化,而现在情况并非如此。

我读过几篇文章,每个人都建议我需要配置Global.asax文件,到目前为止,我已经尝试了几种方法,但都没有成功:

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);
    }

我很确定自己做错了什么,我只是不知道那是什么。

我真的不需要明智地执行此应用程序,也可以配置特定的控制器。

有关该保护对象的一些信息:这是一个MVC5应用程序,在.net Framework 4.7.2中运行

非常感谢!

0 个答案:

没有答案