JsonResult(object) 导致 "The collection type 'Newtonsoft.Json.Linq.JToken' is not supported."
JsonResult(object) causes "The collection type 'Newtonsoft.Json.Linq.JToken' is not supported."
我将现有的 ASP.NET 核心 2.2 项目升级到 3.0。我有一个方法 returns JSON 在 2.2 中有效,但在 3.0 中,它导致 "The collection type 'Newtonsoft.Json.Linq.JToken' is not supported." 在 return.
[HttpGet()]
public async Task<JsonResult> Get()
{
var res = some class object in a third-party library;
return new JsonResult(res);
}
我搜索了Google,发现微软已经将NewtonsoftJson替换为System.Text.Json
,但我并没有明确使用NewtonsoftJson。在项目的"Frameworks"中,我可以看到Newtonsoft Json,我把它和using Newtonsoft.Json.Linq
语句去掉了,但结果是一样的。怎么不用Newtonsoft Json?
错误信息是:
System.NotSupportedException: The collection type 'Newtonsoft.Json.Linq.JToken' is not supported.
at System.Text.Json.JsonClassInfo.GetElementType(Type propertyType, Type parentType, MemberInfo memberInfo, JsonSerializerOptions options)
at System.Text.Json.JsonClassInfo.CreateProperty(Type declaredPropertyType, Type runtimePropertyType, Type implementedPropertyType, PropertyInfo propertyInfo, Type parentClassType, JsonConverter converter, JsonSerializerOptions options)
at System.Text.Json.JsonClassInfo.AddProperty(Type propertyType, PropertyInfo propertyInfo, Type classType, JsonSerializerOptions options)
at System.Text.Json.JsonClassInfo.AddPolicyProperty(Type propertyType, JsonSerializerOptions options)
at System.Text.Json.JsonClassInfo..ctor(Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializerOptions.GetOrAddClass(Type classType)
at System.Text.Json.JsonPropertyInfo.get_ElementClassInfo()
at System.Text.Json.JsonSerializer.HandleObject(JsonPropertyInfo jsonPropertyInfo, JsonSerializerOptions options, Utf8JsonWriter writer, WriteStack& state)
at System.Text.Json.JsonSerializer.WriteObject(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.Infrastructure.SystemTextJsonResultExecutor.ExecuteAsync(ActionContext context, JsonResult result)
at Microsoft.AspNetCore.Mvc.Infrastructure.SystemTextJsonResultExecutor.ExecuteAsync(ActionContext context, JsonResult result)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeResultAsync>g__Logged|21_0(ResourceInvoker invoker, IActionResult result)
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__Logged|17_1(ResourceInvoker invoker)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
PS:我在其他类型上尝试过,并得到了一些类似的集合类型异常消息。于是,我搜索了 Google 并找到了 this open issue on .NET Core's GitHub。似乎 System.Text.JSon
目前不完全支持集合类型,解决方法是使用旧的 Newtonsoft Json.
Migrate from ASP.NET Core 2.2 to 3.0
添加对 Microsoft.AspNetCore.Mvc.NewtonsoftJson
的包引用
更新Startup.ConfigureServices调用AddNewtonsoftJson:
services.AddMvc().AddNewtonsoftJson();
@sabiland 的回答帮助了我,小改动
- 在 .csproj 中添加了对 NewtonsoftJson 的引用,如下所示
< PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0" />
添加到 Startup.cs 方法 ConfigureServices 中,AddMvcCore
services.AddMvcCore()
.AddNewtonsoftJson();
在基于 .NET Core 3.1 的 Web 应用程序中,如果您遇到此错误,您需要的 Nuget 包是相同的 "Microsoft.AspNetCore.Mvc.NewtonsoftJson"。
Startup class 的 ConfigureServices 方法中的代码看起来像
services.AddControllersWithViews().AddNewtonsoftJson();
我将现有的 ASP.NET 核心 2.2 项目升级到 3.0。我有一个方法 returns JSON 在 2.2 中有效,但在 3.0 中,它导致 "The collection type 'Newtonsoft.Json.Linq.JToken' is not supported." 在 return.
[HttpGet()]
public async Task<JsonResult> Get()
{
var res = some class object in a third-party library;
return new JsonResult(res);
}
我搜索了Google,发现微软已经将NewtonsoftJson替换为System.Text.Json
,但我并没有明确使用NewtonsoftJson。在项目的"Frameworks"中,我可以看到Newtonsoft Json,我把它和using Newtonsoft.Json.Linq
语句去掉了,但结果是一样的。怎么不用Newtonsoft Json?
错误信息是:
System.NotSupportedException: The collection type 'Newtonsoft.Json.Linq.JToken' is not supported.
at System.Text.Json.JsonClassInfo.GetElementType(Type propertyType, Type parentType, MemberInfo memberInfo, JsonSerializerOptions options)
at System.Text.Json.JsonClassInfo.CreateProperty(Type declaredPropertyType, Type runtimePropertyType, Type implementedPropertyType, PropertyInfo propertyInfo, Type parentClassType, JsonConverter converter, JsonSerializerOptions options)
at System.Text.Json.JsonClassInfo.AddProperty(Type propertyType, PropertyInfo propertyInfo, Type classType, JsonSerializerOptions options)
at System.Text.Json.JsonClassInfo.AddPolicyProperty(Type propertyType, JsonSerializerOptions options)
at System.Text.Json.JsonClassInfo..ctor(Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializerOptions.GetOrAddClass(Type classType)
at System.Text.Json.JsonPropertyInfo.get_ElementClassInfo()
at System.Text.Json.JsonSerializer.HandleObject(JsonPropertyInfo jsonPropertyInfo, JsonSerializerOptions options, Utf8JsonWriter writer, WriteStack& state)
at System.Text.Json.JsonSerializer.WriteObject(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.Infrastructure.SystemTextJsonResultExecutor.ExecuteAsync(ActionContext context, JsonResult result)
at Microsoft.AspNetCore.Mvc.Infrastructure.SystemTextJsonResultExecutor.ExecuteAsync(ActionContext context, JsonResult result)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeResultAsync>g__Logged|21_0(ResourceInvoker invoker, IActionResult result)
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__Logged|17_1(ResourceInvoker invoker)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
PS:我在其他类型上尝试过,并得到了一些类似的集合类型异常消息。于是,我搜索了 Google 并找到了 this open issue on .NET Core's GitHub。似乎 System.Text.JSon
目前不完全支持集合类型,解决方法是使用旧的 Newtonsoft Json.
Migrate from ASP.NET Core 2.2 to 3.0
添加对 Microsoft.AspNetCore.Mvc.NewtonsoftJson
的包引用
更新Startup.ConfigureServices调用AddNewtonsoftJson:
services.AddMvc().AddNewtonsoftJson();
@sabiland 的回答帮助了我,小改动
- 在 .csproj 中添加了对 NewtonsoftJson 的引用,如下所示
< PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0" />
添加到 Startup.cs 方法 ConfigureServices 中,AddMvcCore
services.AddMvcCore() .AddNewtonsoftJson();
在基于 .NET Core 3.1 的 Web 应用程序中,如果您遇到此错误,您需要的 Nuget 包是相同的 "Microsoft.AspNetCore.Mvc.NewtonsoftJson"。
Startup class 的 ConfigureServices 方法中的代码看起来像
services.AddControllersWithViews().AddNewtonsoftJson();