2015-08-08 52 views
0

在我的asp.net 5 vnext(beta 6)web api項目中,我的輸入/輸出json格式化程序設置似乎沒有任何效果 - 對象的序列化。 我找不到任何地方爲什麼...ASP.Net 5 vnext輸入/輸出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, 「消息」:空}

+0

此鏈接可能有助於http://www.strathweb.com/2014/11/formatters-asp-net-mvc-6/ –

回答

0

我解決了這個代碼的問題:

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

雖然,有了它我不明白的輸入和輸出之間格式化分離的奢侈品。