问题描述
我刚刚将我的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:
根据 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.
有什么想法吗? 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.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放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!