問題描述
我剛剛將我的 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 后出現(xiàn)此錯誤:
After the upgrade to 3.0 I get this error:
錯誤 CS1061 'IMvcBuilder' 不包含'AddJsonOptions' 并且沒有可訪問的擴展方法 'AddJsonOptions'可以找到接受IMvcBuilder"類型的第一個參數(shù)(是您缺少 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?)
根據(jù) 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 去了哪里?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!