Azure Function v3 中的 System.Text.Json:JsonResult.SerializerSettings 必须是 JsonSerializerSettings 类型的实例

Kzr*_*tof 7 c# json.net asp.net-core azure-functions system.text.json

我有一组在 .NET Core 3.1 上运行的 Azure Functions v3。

我需要指定自定义转换器,因此当我在函数中构建 a 时,System.Text.Json我提供了一个自定义实例:JsonSerializerOptionsJsonResult

return new JsonResult(<ContractClass>, NSJsonSerializerOptions.Default)
{
    StatusCode = StatusCodes.Status200OK
};
Run Code Online (Sandbox Code Playgroud)

问题

我收到以下错误,并且我不确定 Newtonsoft 来自哪里,因为 ASP.NET Core 应该使用System.Text.Json

Microsoft.AspNetCore.Mvc.NewtonsoftJson: Property 'JsonResult.SerializerSettings' must be an instance of type 'Newtonsoft.Json.JsonSerializerSettings'.
Run Code Online (Sandbox Code Playgroud)

更新

我发现该JsonResult实例正在寻找 的实现IActionResultExecutor<JsonResult>,并得到一个NewtonsoftJsonresultExecutor而不是SystemTextJsonResultExecutorJsonResult这是的方法的代码ExecuteResultAsync

Json结果

我认为 Azure Function 将依赖于 ASP.Net Core,而 ASP.Net Core 又依赖于System.Text.Json.

小智 -2

您的序列化项目必须继承自JsonSerializerSettings

 new { Testc = "test" },
 new JsonSerializerOptions()
 {
     PropertyNameCaseInsensitive = true,
     //ContractResolver = new CamelCasePropertyNamesContractResolver(),
     //ReferenceLoopHandling = ReferenceLoopHandling.Ignore
}
Run Code Online (Sandbox Code Playgroud)