OnDeserialized for JSON导致对象为null

时间:2014-07-03 20:31:50

标签: asp.net-web-api json.net deserialization

我的WebAPI项目上有一条路由,它接受一个对象作为输入ExportPostDataExportPostData有一个类型为Contract的名为“contract”的属性,当我调用该路由时,该属性已成功填充。我将[OnDeserialized]标记添加到Contract类,现在它始终无法反序列化。没有抛出错误,只有Contract为空。我不知道如何调试这个,因为我的OnDeserialized方法永远不会被击中。

ExportPostData

public class ExportPostData
{
    public Contract contract { get; set; }
    public bool includeSubItems { get; set; }
    public string user { get; set; }
    public string[] projects { get; set; }
}

合同

public class ZEstimateContract
{
    public string _id { get; set; }
    public string contractName { get; set; }
    public string contractNumber { get; set; }
    public string updatedBy { get; set; }
    public DateTime updated_at { get; set; }

    [OnDeserialized()]
    internal void Deserialized()
    {
        // THIS NEVER GETS HIT 
        Console.WriteLine("I'm deserialized");
    }
}

1 个答案:

答案 0 :(得分:4)

更改

[OnDeserialized()]
internal void Deserialized()
{
    // THIS NEVER GETS HIT 
    Console.WriteLine("I'm deserialized");
}

到此:

    [OnDeserialized]
    internal void Deserialized(StreamingContext context)
    {
        // THIS GETS HIT NOW
        Console.WriteLine("I'm deserialized");
    }

如果没有参数,方法的签名与OnDeserialized正在查找的内容不匹配。有关详细信息,请参阅此文章:http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializedattribute.aspx