zoukankan      html  css  js  c++  java
  • asp.net core 3.0 JObject The collection type 'Newtonsoft.Json.Linq.JObject' is not supported

    在asp.net core 3.0 中,如果直接在Controller中返回 Jobject 类型,会抛出如下错误:

    
    The collection type 'Newtonsoft.Json.Linq.JObject' is not supported.
    System.NotSupportedException: The collection type 'Newtonsoft.Json.Linq.JObject' is not supported.
    at System.Text.Json.JsonPropertyInfoNotNullable`4.GetDictionaryKeyAndValueFromGenericDictionary(WriteStackFrame& writeStackFrame, String& key, Object& value)
    at System.Text.Json.JsonPropertyInfo.GetDictionaryKeyAndValue(WriteStackFrame& writeStackFrame, String& key, Object& value)
    at System.Text.Json.JsonSerializer.HandleDictionary(JsonClassInfo elementClassInfo, JsonSerializerOptions options, Utf8JsonWriter writer, WriteStack& state)
    at System.Text.Json.JsonSerializer.Write(Utf8JsonWriter writer, Int32 originalWriterDepth, Int32 flushThreshold, JsonSerializerOptions options, WriteStack& state)
    at System.Text.Json.JsonSerializer.WriteAsyncCore(Stream utf8Json, Object value, Type inputType, JsonSerializerOptions options, CancellationToken cancellationToken)
    at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
    at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
    --- End of stack trace from previous location where exception was thrown ---
    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
    at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
    
    

    该问题的出现估计与.net 3.0 新引入的System.Text.Json类库有关.

    折衷的解决办法是:

    使用Content方法将 JObject 类型的返回值转为 ContentResult 类型.

    伪代码如下:

    
    [HttpPost]
    
    public ContentResult Method1([FromBody]Param param1)
    {
    
      JObject result=xxx;
    
      return Content(result.ToString());
    
    }
    
    

    如果有时间,建议跟一下System.Text.Json类库的源码来彻底解决该问题.

  • 相关阅读:
    FastDFS迁移步骤
    Centos7 单节点安装 FastDFS + FastDHT服务
    CentOS 7.0 上安装和配置 VNC 服务器
    Ubuntu 18.04 安装 Xfce桌面和VNC的方法
    Ubuntu 16.04设置root用户登录图形界面
    Windows批量查找文件
    WIN10打开网络共享文件夹提示0x80004005怎么解决?(转载)
    ack 工具
    win7/win10 未分配磁盘怎样创建扩展分区 也就是逻辑分区(转截)
    [bzoj4842][bzoj1283][Neerc2016]Delight for a Cat/序列_线性规划_费用流
  • 原文地址:https://www.cnblogs.com/rsls/p/12100971.html
Copyright © 2011-2022 走看看