服务AddJsonOptions .net core 2.1 [英] services AddJsonOptions .net core 2.1

查看:81
本文介绍了服务AddJsonOptions .net core 2.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前,在.net 2.0中,您可以通过这种方式添加json内容

Previously with .net 2.0, you can add json stuffs this way

 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做同样的事情.

I realize I can't do the same thing with .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

有人有解决办法吗?

推荐答案

使用

services.AddMvc().AddJsonOptions(...)

进行配置.

以上扩展方法可以在 Microsoft.AspNetCore.Mvc.Formatters.Json中找到版本2.1.0.0 .要么直接包含此软件包,要么添加这两个 Microsoft.AspNetCore.App / Microsoft.AspNetCore.All 之一.

Above extension method can be found in Microsoft.AspNetCore.Mvc.Formatters.Json Version 2.1.0.0. Either include this package directly, or add one of these two Microsoft.AspNetCore.App / Microsoft.AspNetCore.All.

这篇关于服务AddJsonOptions .net core 2.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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