2016-09-26 25 views
2

在asp.net mvc 5中对HttpClient使用PutAsJsonAsync扩展方法返回自检参考循环检测到的异常。使用HttpClient PutAsJsonAsync Extension检测到的自回参考循环

这里是调用代码:

httpClient.BaseAddress = _uri; 
HttpResponseMessage response = await httpClient.PutAsJsonAsync<b>("index/1",b); 
response.EnsureSuccessStatusCode(); 

对象B不会具有自参考。

所以我的问题是如何在一个asp.net mvc 5应用程序中设置SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore

回答

2

解决此问题的一种方法是从使用PutAsJsonAsync扩展方法更改为使用PutAsync扩展方法并明确设置MediaTypeformatter。

var jsonformatter = new JsonMediaTypeFormatter(); 
jsonformatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; 

HttpResponseMessage response = await httpClient.PutAsync<b>("index/1",b,jsonformatter); 
response.EnsureSuccessStatusCode(); 

这允许您使用任何您需要的设置。