从 .NET Core 2.2 迁移到 3.0 后缺少 AddJsonFormatters()

AddJsonFormatters() missing after migration from .NET Core 2.2 to 3.0

我有一个 Net Core 2.2 Web Api,我通过集成 IdentityServer4 来保护它。所以我从 IDS4 的教程开始编写代码,在那里我找到了 AddJsonFormatters()。

我正在尝试将它从 .NET Core 2.2 迁移到 .NET Core 3.0。

目前我在 ConfigureServices() 的编译时遇到问题。

我没有找到 AddJsonFormatters(),如果我理解正确,我必须使用 AddMvcOptions() 来获得相同的结果。

这是正确的吗?在这种情况下,等效配置是什么?

// .NET Core 2.2
public void ConfigureServices(IServiceCollection services)
{
    services.AddMvcCore()
            .AddAuthorization()
            .AddJsonFormatters();

    // Other code...
}

// .NET Core 3.0
public void ConfigureServices(IServiceCollection services)
{
    services.AddMvcCore()
            .AddAuthorization()
            // Something like this...
            .AddMvcOptions(options =>
            {                       
                //options.OutputFormatters.Add(new SomeKindOf_IOutputFormatter());
                //options.InputFormatters.Add(new SomeKindOf_IInputFormatter(options));
            });

    // Other code...
}

我刚刚发现 IdentityServer4 正在慢慢更新他们的 .NET Core 3.0 样本。附件是 link 您所询问部分的较新版本代码,希望对您有所帮助。 https://github.com/IdentityServer/IdentityServer4/blob/master/samples/Quickstarts/1_ClientCredentials/src/Api/Startup.cs

您可以使用 Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet 包,并在 Startup.cs 中配置它:

services.AddMvcCore()    
    .AddNewtonsoftJson(o =>
    {
        o.SerializerSettings.Converters.Add(new StringEnumConverter());
        o.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    })