Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 迁移到.NET Core 3.1-缺少Netwonsoft_C#_Json_Asp.net Core_Json.net - Fatal编程技术网

C# 迁移到.NET Core 3.1-缺少Netwonsoft

C# 迁移到.NET Core 3.1-缺少Netwonsoft,c#,json,asp.net-core,json.net,C#,Json,Asp.net Core,Json.net,我刚从2.2迁移到ASP.NET Core 3.1,出现以下错误: System.NotSupportedException: The collection type 'System.Collections.Generic.Dictionary`2[System.Object,System.Object]' is not supported. at System.Text.Json.JsonClassInfo.GetElementType(Type propertyType, Type p

我刚从2.2迁移到ASP.NET Core 3.1,出现以下错误:

System.NotSupportedException: The collection type 'System.Collections.Generic.Dictionary`2[System.Object,System.Object]' 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.WriteStackFrame.Initialize(Type type, JsonSerializerOptions options)
   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)
但是,我安装了这个包
Microsoft.AspNetCore.Mvc.NewtonsoftJson
,并且在启动时添加了
services.AddControllers().AddNewtonsoftJson()

那么,为什么Newtonsoft仍然被忽略而使用System.Text.Json呢

编辑: 代码抛出错误:

public async Task<Dictionary<object, object>> GetTemplatesDictAsync(
           int? from = 0,
            int? take = 100,
            string search = null)
        {
            var _templates = await _repository.GetAllAsync(from, take, search, );

            var _dict = _templates.ToDictionary(t => (object)t.id, t => (object)t);

            // also append a property with original list
            _dict.Add("list", _templates);

            return _dict;
        }

在.NET Core 3+项目中,您有一组不同的调用来替换MVC。因此,您可能会遇到以下情况之一:

services.AddControllers().AddNewtonsoftJson();
services.AddControllersWithViews().AddNewtonsoftJson();
services.AddRazorPages().AddNewtonsoftJson();
如果这不起作用,请告诉我们您在哪里使用它


您可以找到一种更为循序渐进的方法,它对我很有效。

在.NET Core 3+项目中,您有一组不同的调用来取代MVC。因此,您可能会遇到以下情况之一:

services.AddControllers().AddNewtonsoftJson();
services.AddControllersWithViews().AddNewtonsoftJson();
services.AddRazorPages().AddNewtonsoftJson();
如果这不起作用,请告诉我们您在哪里使用它


您可以找到一种更为循序渐进的方法,它对我很有用。

我添加了
services.AddControllers().AddNewtonsoftJson()-我在问题中写的。我只是想知道为什么默认序列化程序仍然在使用请向我们展示引发错误的代码。请确保您的NewtonsoftJson包已更新为3.1。也请检查我编辑的答案。我添加了
services.AddControllers().AddNewtonsoftJson()-我在问题中写的。我只是想知道为什么默认序列化程序仍然在使用请向我们展示引发错误的代码。请确保您的NewtonsoftJson包已更新为3.1。也请检查我编辑的答案。我可以用它。你的startup.cs是什么样子的?请参阅Edit2。谢谢你的startup.cs是什么样子的?请参见Edit2。谢谢