【问题标题】:AddJsonFormatters() missing after migration from .NET Core 2.2 to 3.0从 .NET Core 2.2 迁移到 3.0 后缺少 AddJsonFormatters()
【发布时间】:2020-02-19 20:44:50
【问题描述】:

我有一个 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...
}

【问题讨论】:

  • 在 3.0 中,AddMvcCore 会自动注册 SystemTextJsonInputFormatter 和 SystemTextJsonOutputFormatter,因此您不需要为此调用。现在首选的方法似乎是使用AddControllers 等,但仍然支持AddMvcCore。
  • @KirkLarkin 我删除了旧的 2.2 代码,只使用了 AddControllers(),现在它可以工作了。

标签: identityserver4 .net-core-3.0 asp.net-core-3.0


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 是的,IdentityServer4 有一组新的示例,所以我按照新示例进行操作,它可以工作。主要区别在于使用 AddControllers() 而不是 services.AddMvcCore().AddAuthorization().AddFormatters();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-05
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多