Asp.net mvc 5 使用HttpClient PutAsJsonAsync扩展检测到自引用循环

Asp.net mvc 5 使用HttpClient PutAsJsonAsync扩展检测到自引用循环,asp.net-mvc-5,dotnet-httpclient,Asp.net Mvc 5,Dotnet Httpclient,在asp.net mvc 5中使用HttpClient的PutAsJsonAsync扩展方法会返回一个自引用循环检测到的异常 以下是呼叫代码: httpClient.BaseAddress = _uri; HttpResponseMessage response = await httpClient.PutAsJsonAsync<b>("index/1",b); response.EnsureSuccessStatusCode(); httpClient.BaseAddress=\

在asp.net mvc 5中使用HttpClient的PutAsJsonAsync扩展方法会返回一个自引用循环检测到的异常

以下是呼叫代码:

httpClient.BaseAddress = _uri;
HttpResponseMessage response = await httpClient.PutAsJsonAsync<b>("index/1",b);
response.EnsureSuccessStatusCode();
httpClient.BaseAddress=\u uri;
HttpResponseMessage response=等待httpClient.PutAsJsonAsync(“索引/1”,b);
response.EnsureSuccessStatusCode();
对象b确实具有自引用


因此,我的问题是如何在asp.net mvc 5应用程序中设置
SerializerSettings.ReferenceLoopHandling=ReferenceLoopHandling.Ignore

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

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

HttpResponseMessage response = await httpClient.PutAsync<b>("index/1",b,jsonformatter);
response.EnsureSuccessStatusCode();
var-jsonformatter=new-JsonMediaTypeFormatter();
jsonformatter.SerializerSettings.ReferenceLoopHandling=ReferenceLoopHandling.Ignore;
HttpResponseMessage response=等待httpClient.PutAsync(“索引/1”,b,jsonformatter);
response.EnsureSuccessStatusCode();

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

您可以在第页查看我的答案。