问题描述
我刚刚将我的 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 指令或程序集引用?)
根据 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 的重大更改的公告.
ASP.NET Core 3.0 和 .NET Core 3.0 包含不同的 JSON API,而不是 Json.NET,它更加注重性能.您可以在关于.NET Core 3.0 中 JSON 的未来"的公告中了解更多信息.
services.AddControllers()
.AddNewtonsoftJson();
services.AddControllers()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
这篇关于.Net Core 3.0 中的 IMvcBuilder AddJsonOptions 去了哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!