在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类库的源码来彻底解决该问题.

最新文章

  1. 锋利的jQuery--表单等(读书笔记三)
  2. JS移动端如何监听软键盘回车事件
  3. IOS 登陆界面的简单编写(通过NSNotificationCenter)
  4. Android HTTP session &amp;&amp; cookie
  5. mybatis源码学习: 编译的方法
  6. iOS UILable高度自适应
  7. textarea限制字符数
  8. Spring Mvc 笔记二之异常和文件上传
  9. gcc命令以及makefile文件
  10. Express使用html模板
  11. [c#]asp.net开发微信公众平台(6)阶段总结、服务搭建、接入
  12. light oj 1027 A Dangerous Maze
  13. Weinre 远程调试移动端页面
  14. Protobuf动态解析在Java中的应用 包含例子程序
  15. python编码错误的解决办法 SyntaxError: Non-ASCII character &#39;\xe5&#39; in file
  16. 如何使用 OpenCV 打开摄像头获取图像数据?
  17. PHP Yii2 composer环境安装
  18. B. Vova and Trophies 字符串预处理+思维+贪心
  19. Linux测试硬盘读性能的常用工具-hdparm和dd俩搭档
  20. 3D数学基础(二)向量

热门文章

  1. Liferay OSGi注解(Annotation) - 使用手册(译文)
  2. 在 Linux Mint 19 上安装 zsh 和设置小键盘一步到位
  3. cume_dist(),名次分析——-最大排名/总个数
  4. 8.5打包libgdx为一个桌面程序(jar包)
  5. 前后端登录注册之node剖析与token的使用状态
  6. HDU-4807-Lunch Time(二分+费用流,思维)
  7. Facebook F8|闲鱼高级技术专家参会分享
  8. OpenStack☞HTTP协议
  9. Laravel5.2 发送邮件(smtp方式最简单的讲解!)-邮件部分
  10. AUTOSSH设置ssh隧道,实现反向代理访问内网主机