欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net core 3.0 JObject The collection type 'Newtonsoft.Json.Linq.JObject' is not supported

程序员文章站 2022-06-14 10:13:57
在asp.net core 3.0 中,如果直接在 中返回 类型,会抛出如下错误: The collection type 'Newtonsoft.Json.Linq.JObject' is not supported. System.NotSupportedException: The colle ......

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