.Net Core 3.0 中的 IMvcBuilder AddJsonOptions 去了哪里?

Where did IMvcBuilder AddJsonOptions go in .Net Core 3.0?(.Net Core 3.0 中的 IMvcBuilder AddJsonOptions 去了哪里?)

本文介绍了.Net Core 3.0 中的 IMvcBuilder AddJsonOptions 去了哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将我的 ASP Web API 项目从 .Net core 2.0 升级到 3.0.我正在使用

I've just upgraded my ASP web API project from .Net core 2.0 to 3.0. I was using

     services.AddMvc()
             .AddJsonOptions(options =>options.SerializerSettings.ContractResolver 
                                       = new DefaultContractResolver());

以前是为了确保序列化 JSON 的小写.

previously to ensure lower-casing of the serialized JSON.

升级到 3.0 后出现此错误:

After the upgrade to 3.0 I get this error:

错误 CS1061 'IMvcBuilder' 不包含'AddJsonOptions' 并且没有可访问的扩展方法 'AddJsonOptions'可以找到接受IMvcBuilder"类型的第一个参数(是您缺少 using 指令或程序集引用?)

Error CS1061 'IMvcBuilder' does not contain a definition for 'AddJsonOptions' and no accessible extension method 'AddJsonOptions' accepting a first argument of type 'IMvcBuilder' could be found (are you missing a using directive or an assembly reference?)

根据 AddJsonOptions for MvcJsonOptions in Asp.Net Core 2.2 AddJsonOptions 扩展方法由 Microsoft.AspNetCore.Mvc.Formatters.Json nuget 包提供.我已经尝试安装/重新安装它,但仍然无法解决该方法.有趣的是,即使我添加了 Json nuget 包,当我尝试添加 using 语句时,智能感知仅显示 Microsoft.AspNetCore.Mvc.Formatters.Xml.

According to AddJsonOptions for MvcJsonOptions in Asp.Net Core 2.2 the AddJsonOptions extension method is/was provided by the Microsoft.AspNetCore.Mvc.Formatters.Json nuget package. I have tried installing/reinstalling this but still can't resolve the method. Interestingly, intellisense only shows Microsoft.AspNetCore.Mvc.Formatters.Xml when I try to add the using statement even though I added the Json nuget package.

有什么想法吗?AddJsonOptions 的 noreferrer">文档 仅适用于 .Net 2.2,因此该方法在 3.0 中可能已被弃用,而支持其他一些配置机制?

Any ideas what is going on? The documentation for AddJsonOptions only goes up to .Net 2.2 so perhaps the method has been deprecated in 3.0 in favor of some other configuration mechanism?

推荐答案

作为 ASP.NET Core 3.0 的一部分,团队默认不再包含 Json.NET.您可以在 关于 Microsoft.AspNetCore.App 的重大更改的公告.

As part of ASP.NET Core 3.0, the team moved away from including Json.NET by default. You can read more about that in general in the announcement on breaking changes to Microsoft.AspNetCore.App.

ASP.NET Core 3.0 和 .NET Core 3.0 包含不同的 JSON API,而不是 Json.NET,它更加注重性能.您可以在关于.NET Core 3.0 中 JSON 的未来"的公告中了解更多信息.

Instead of Json.NET, ASP.NET Core 3.0 and .NET Core 3.0 include a different JSON API that focuses a bit more on performance. You can learn about that more in the announcement about "The future of JSON in .NET Core 3.0".

ASP.NET Core 的新模板将不再与 Json.NET 捆绑,但您可以轻松地重新配置项目以使用它而不是新的 JSON 库.这对于与旧项目的兼容性都很重要,也因为新库不应该完全替代,因此您不会在那里看到完整的功能集.

The new templates for ASP.NET Core will no longer bundle with Json.NET but you can easily reconfigure the project to use it instead of the new JSON library. This is important for both compatibility with older projects and also because the new library is not supposed to be a full replacement, so you won't see the full feature set there.

为了使用 Json.NET 重新配置您的 ASP.NET Core 3.0 项目,您需要添加对 Microsoft.AspNetCore.Mvc.NewtonsoftJson 的 NuGet 引用,该包包含所有必要的位.然后,在 Startup 的 ConfigureServices 中,您需要像这样配置 MVC:

In order to reconfigure your ASP.NET Core 3.0 project with Json.NET, you will need to add a NuGet reference to Microsoft.AspNetCore.Mvc.NewtonsoftJson, which is the package that includes all the necessary bits. Then, in the Startup’s ConfigureServices, you will need to configure MVC like this:

services.AddControllers()
    .AddNewtonsoftJson();

这会设置 MVC 控制器并将其配置为使用 Json.NET 而不是新的 API.除了控制器,您还可以使用不同的 MVC 重载(例如,用于具有视图的控制器或 Razor 页面).AddNewtonsoftJson 方法有一个重载,允许您像在 ASP.NET Core 2.x 中使用 AddJsonOptions 一样配置 Json.NET 选项.

This sets up MVC controllers and configures it to use Json.NET instead of that new API. Instead of controllers, you can also use a different MVC overload (e.g. for controllers with views, or Razor pages). That AddNewtonsoftJson method has an overload that allows you to configure the Json.NET options like you were used to with AddJsonOptions in ASP.NET Core 2.x.

services.AddControllers()
    .AddNewtonsoftJson(options =>
    {
        options.SerializerSettings.ContractResolver = new DefaultContractResolver();
    });

这篇关于.Net Core 3.0 中的 IMvcBuilder AddJsonOptions 去了哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:.Net Core 3.0 中的 IMvcBuilder AddJsonOptions 去了哪里?