asp.net core 3.0 JObject The collection type 'Newtonsoft.Json.Linq.JObject' is not supported
2024-09-06 18:15:44
在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
类库的源码来彻底解决该问题.
最新文章
- 锋利的jQuery--表单等(读书笔记三)
- JS移动端如何监听软键盘回车事件
- IOS 登陆界面的简单编写(通过NSNotificationCenter)
- Android HTTP session &;&; cookie
- mybatis源码学习: 编译的方法
- iOS UILable高度自适应
- textarea限制字符数
- Spring Mvc 笔记二之异常和文件上传
- gcc命令以及makefile文件
- Express使用html模板
- [c#]asp.net开发微信公众平台(6)阶段总结、服务搭建、接入
- light oj 1027 A Dangerous Maze
- Weinre 远程调试移动端页面
- Protobuf动态解析在Java中的应用 包含例子程序
- python编码错误的解决办法 SyntaxError: Non-ASCII character &#39;\xe5&#39; in file
- 如何使用 OpenCV 打开摄像头获取图像数据?
- PHP Yii2 composer环境安装
- B. Vova and Trophies 字符串预处理+思维+贪心
- Linux测试硬盘读性能的常用工具-hdparm和dd俩搭档
- 3D数学基础(二)向量
热门文章
- Liferay OSGi注解(Annotation) - 使用手册(译文)
- 在 Linux Mint 19 上安装 zsh 和设置小键盘一步到位
- cume_dist(),名次分析——-最大排名/总个数
- 8.5打包libgdx为一个桌面程序(jar包)
- 前后端登录注册之node剖析与token的使用状态
- HDU-4807-Lunch Time(二分+费用流,思维)
- Facebook F8|闲鱼高级技术专家参会分享
- OpenStack☞HTTP协议
- Laravel5.2 发送邮件(smtp方式最简单的讲解!)-邮件部分
- AUTOSSH设置ssh隧道,实现反向代理访问内网主机