ASP.Net 5 vnext输入/输出json格式化程序不起作用

时间:2015-08-08 21:06:01

标签: c# asp.net asp.net-web-api asp.net-core

在我的asp.net 5 vnext(beta 6)web api项目中,我的输入/输出json格式化程序设置似乎对对象的序列化没有任何影响。 我找不到任何原因......

有没有人遇到过这个?

    public void ConfigureServices(IServiceCollection services)
    {
        // Add MVC services.
        services.AddMvc();

        // Configure.
        services.ConfigureMvc(o =>
        {
            // Input Formatters.
            var jif = new JsonInputFormatter()
            {
                SerializerSettings = new JsonSerializerSettings()
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver(),
                    DefaultValueHandling = DefaultValueHandling.Ignore,
                    NullValueHandling = NullValueHandling.Ignore
                }
            };

            o.InputFormatters.Clear();
            o.InputFormatters.Add(jif);

            // Output Formatters.
            var jof = new JsonOutputFormatter()
            {
                SerializerSettings = new JsonSerializerSettings()
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver(),
                    DefaultValueHandling = DefaultValueHandling.Ignore,
                    NullValueHandling = NullValueHandling.Ignore
                }
            };

            o.OutputFormatters.Clear();
            o.OutputFormatters.Add(jof);
        });
    }

通过这些设置,我仍然可以将其作为服务器响应:

{ “结果”: “错误”, “理”:空, “时间”:1439068137652, “消息”:空}

1 个答案:

答案 0 :(得分:0)

我用这段代码解决了这个问题:

        services.ConfigureMvcJson(o =>
        {
            o.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            o.SerializerSettings.DefaultValueHandling = DefaultValueHandling.Ignore;
            o.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
        });

尽管如此,我并没有在输入和输出格式化器之间分离。