以前,在.net 2.0中,您可以通过这种方式添加json内容
services.AddJsonOptions(options => {
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
或
services.AddMvc().AddJsonOptions(options => {
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
我意识到我无法使用.net 2.1做同样的事情。
我收到此错误:
'IServiceCollection' does not contain a definition for 'AddJsonOptions'
and the best extension method overload 'MvcJsonMvcBuilderExtensions.AddJsonOptions(IMvcBuilder, Action<MvcJsonOptions>)'
requires a receiver of type 'IMvcBuilder
有人解决吗?
答案 0 :(得分:9)
使用
services.AddMvc().AddJsonOptions(...)
进行配置。
上述扩展方法可以在Microsoft.AspNetCore.Mvc.Formatters.Json
Version 2.1.0.0
中找到。要么直接包含此软件包,要么添加这两个Microsoft.AspNetCore.App
/ Microsoft.AspNetCore.All
之一。
答案 1 :(得分:0)
发现问题-您需要确保已参考
Microsoft.AspNetCore.Mvc