当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net core 3.0 JObject The collection type 'Newtonsoft.Json.Linq.JObject' is not supported

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

2019年12月26日  | 移动技术网IT编程  | 我要评论

网页回拨电话,猿女,泸州小蚂蚁信息网

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网