当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net web api - JsonFormatter in WebApi is ignored

问题描述:

I need to configure the JSON output of a Web API.

It works, when I directly set the JsonProperty-Attribute on properties.

But changes in the settings of the JsonFormatter (in the Register-method) are completely ignored.

 public static void Register(HttpConfiguration config)

{

// Web API configuration and services

config.Formatters.JsonFormatter.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;

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

// ...

}

What am I doing wrong?

网友答案:

My fault.

In the WebApi I talked to an Azure DocumentDB.

So I had - of course - configure the DocumentDB serializer to format the Json documents.

    static DocumentDbRepository()
    {


        JsonConvert.DefaultSettings = () =>
            {
                return new JsonSerializerSettings
                           {
                               NullValueHandling = NullValueHandling.Ignore,
                               TypeNameHandling = TypeNameHandling.Objects,  

                               //serializing works, but Linq to DocumentDB provider seems to ignore the ContractResolver. -> [JsonProperty]
                               //ContractResolver = new CamelCasePropertyNamesContractResolver(),
                           };
            };


    }
分享给朋友:
您可能感兴趣的文章:
随机阅读: