gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:09:47 25 4
gpt4 key购买 nike

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

调用代码如下:

httpClient.BaseAddress = _uri;
HttpResponseMessage response = await httpClient.PutAsJsonAsync<b>("index/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();

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

关于asp.net-mvc-5 - 使用 HttpClient PutAsJsonAsync 扩展检测到自引用循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39707555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com