序列化null导航属性

时间:2014-07-31 05:49:42

标签: asp.net-web-api odata asp.net-web-api2

我有简单的课程:

public class Person
{
   public string Name {get; set;}
   public Address Address {get; set;} // can be null
}

当我查询Person时,我希望webapi返回空的Address属性。我尝试了以下内容:

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = new Newtonsoft.Json.JsonSerializerSettings()
            {
                NullValueHandling = Newtonsoft.Json.NullValueHandling.Include,

            };

但是,没有任何变化,它只是在结果中不存在。

1 个答案:

答案 0 :(得分:0)

您无需配置格式化程序,只需添加$ expand query选项:

GET ~/Person?$expand=Address

我试过,它有效。我正在使用WebApi OData for OData v4。

{
  "@odata.context":"http://jinfutan13:9123/$metadata#Albums/$entity",
  "ID":5,"Name":"Name 5",
  "Singer":null
}

歌手也是导航财产。

相关问题