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

一个 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)

请问如何解决?

回答

通过添加 [JsonNumberHandling] attribute 解决了

public class ResultDocument
{
    [JsonNumberHandling(JsonNumberHandling.AllowNamedFloatingPointLiterals)]
    public float Score { get; set; }
}
以上是ASP.NET Core 报错 “.NET number values such as positive and negative infinity cannot be written as valid JSON”的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>