首页 新闻 会员 周边 捐助

ASP.NET Core 报错 ".NET number values such as positive and negative infinity cannot be written as valid JSON"

0
悬赏园豆:30 [已解决问题] 解决于 2022-04-21 09:30

一个 ASP.NET Core 6.0 Web API 项目运行时日志中报错

System.ArgumentException: .NET number values such as positive and negative infinity cannot be written as valid JSON.
   at System.Text.Json.ThrowHelper.ThrowArgumentException_ValueNotSupported()
   at System.Text.Json.Utf8JsonWriter.WriteNumberValue(Single value)

请问如何解决?

dudu的主页 dudu | 高人七级 | 园豆:29759
提问于:2022-04-21 08:12
< >
分享
最佳答案
0

通过添加 [JsonNumberHandling] attribute 解决了

public class ResultDocument
{
    [JsonNumberHandling(JsonNumberHandling.AllowNamedFloatingPointLiterals)]
    public float Score { get; set; }
}
dudu | 高人七级 |园豆:29759 | 2022-04-21 09:29
其他回答(1)
0

https://stackoverflow.com/questions/7814247/serialize-nan-values-into-json-as-nulls-in-json-net

收获园豆:30
talentzemin | 园豆:775 (小虾三级) | 2022-04-21 09:16

用的是 System.Text.Json

支持(0) 反对(0) dudu | 园豆:29759 (高人七级) | 2022-04-21 09:28
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册