.netcore 获取appsettings

我的开发环境是abpvnext net6.0 。

因为业务需要,从原来老项目net4.5工程里复制了一个报表导出的业务类到net6项目里面,但是他的获取appsettings的代码其实不用想都知道会报错。因为原来framwork时代获取appsettings的方法常见的是 System.Configuration.ConfigurationManager.AppSettings"".ToString()这种。肯定在netcore里面不适用。报错Object reference not set to an instance of an object.

然后我到网上查了好多链接,也没有一个准确的说法。通常情况下我们在.NETCORE里面使用IConfiguration 获取配置文件都是些到startup文件里面 ,通过注入的方式来使用IConfiguration 。但是单业务类里面没法注入和构造函数实例化IConfiguration ,就必须要用new的方式来解决问题,最后到技术群里得到大佬指点,就一行代码解决

IConfiguration myconfiguration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json").Build();

代码截图如下图:

相关推荐
geovindu17 小时前
CSharp:Condition Variable Pattern
后端·设计模式·c#·.net·.netcore·条件变量模式·同步型模式
geovindu5 天前
CSharp: Observer Pattern
开发语言·后端·观察者模式·设计模式·c#·.netcore·行为模式
geovindu6 天前
CSharp: Strategy Pattern
开发语言·后端·c#·.net·.netcore·策略模式·行为模式
geovindu6 天前
CSharp: Task Scheduler
开发语言·后端·c#·.net·.netcore
geovindu12 天前
CSharp: Wordcloud
开发语言·后端·c#·.net·.netcore·词云
geovindu12 天前
CSharp: Command Pattern
开发语言·后端·c#·.net·.netcore·命令模式·行为模式
全栈小519 天前
【C#】.net core,静态方法线程安全解析,面试时经常被问的线程安全就藏在里面
安全·c#·.netcore
清风与日月22 天前
Yitter.IdGenerator:高性能分布式唯一ID生成器详解
分布式·c#·.net·.netcore
Lost of 程序猿24 天前
ASP.NET Core 后台任务全景:从 BackgroundService 到 Channel 队列,再到分布式调度
后端·asp.net·.netcore
宝桥南山25 天前
Blazor Web Assembly - 体验一下Authentication State从Server共享给Client
microsoft·微软·c#·asp.net·.net·.netcore