.Net Core 3.0中IMvcBuilder AddJsonOptions放在哪里? [英] Where did IMvcBuilder AddJsonOptions go in .Net Core 3.0?

查看:1124
本文介绍了.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?)

根据 Asp.Net Core 2.2中MvcJsonOptions的AddJsonOptions AddJsonOptions扩展方法由 Microsoft.AspNetCore.Mvc.Formatters.Json nuget包提供.我已经尝试安装/重新安装此程序,但仍然无法解决该方法.有趣的是,即使我添加了 Json nuget包,当我尝试添加using语句时,intellisense仍只显示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.

有什么想法吗?

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.NET,包括一个不同的JSON API,该API更加注重性能.您可以在关于".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引用,该包包括所有必要的位.然后,在启动公司的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方法有一个重载,使您可以配置Json.NET选项,就像在ASP.NET Core 2.x中使用AddJsonOptions一样.

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放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆