2015-10-17 97 views
1

我正在从我的服务中调用我的Web Api控制器。我的模型对象正确传递,但在控制器中我的对象导航属性丢失。Web Api序列化或反序列化问题

这里是我的模型结构

public class ClassA 
{ 
    public String PropA { get; set; } 
    public String PropB { get; set; } 
    public virtual ClassB PropClassB { get; set; } 
} 

public class ClassB 
{ 
    public string PropC { get; set; } 
    public bool PropD { get; set; } 
} 

,我的方法,其呼叫控制器是

private void publishReport(T perameter) 
{ 
    using (HttpClient client = new HttpClient()) 
    { 
     client.BaseAddress = new Uri(ConfigurationManager.AppSettings["GovernmentApiBaseUrl"]); 
     client.DefaultRequestHeaders.Accept.Clear(); 
     client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

     HttpResponseMessage response = client.PostAsJsonAsync<T>(_apiUrl, perameter).Result; // here I have a value in navigation property 
    } 
} 

这里是我的控制器

[HttpPost] 
public HttpResponseMessage PublishPOBReport([FromBody] ClassA parameter) // here parameter have all the values but the navigation property is null 
{ 
    return new HttpResponseMessage() { StatusCode = HttpStatusCode.OK }; 
} 
+0

可以确认JSON包含'ClassB'?我有'native .net json serializer <-> Json.NET'(Web API使用作为默认)通信的一些问题,因此尝试在两边都使用Json.NET。 – ieaglle

+0

以上代码在我的机器上工作完美(可以Serialization或Deserialization nav属性),我认为问题不在于上面的代码。 –

+0

我高度怀疑你使用延迟加载功能可以从英孚获得的记录,请尝试使用[急切装填]获取记录(https://msdn.microsoft.com/en-us/data/jj574232.aspx),并在再次检查最终 –

回答