.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();

代码截图如下图:

相关推荐
武藤一雄14 小时前
.NET 中常见计时器大全
microsoft·微软·c#·.net·wpf·.netcore
武藤一雄2 天前
[.NET] 中 System.Collections.Generic命名空间详解
windows·微软·c#·asp.net·.net·.netcore
van久5 天前
.Net Core 学习:Razor Pages中 HTML 表头字段的两种写法对比
学习·html·.netcore
武藤一雄6 天前
C# 万字拆解线程间通讯?
后端·微软·c#·.net·.netcore·多线程
武藤一雄7 天前
.NET中到底什么是SignalR (持续更新)
后端·微软·c#·asp.net·.net·.netcore·signalr
by__csdn7 天前
第二章 (.NET Core环境搭建)第二节( Visual Studio Code)
ide·vscode·c#·vue·asp.net·.net·.netcore
by__csdn7 天前
第二章 (.NET Core环境搭建)第三节( Visual Studio for Mac)
ide·kubernetes·c#·asp.net·.net·.netcore·visual studio
武藤一雄8 天前
C#:进程/线程/多线程/AppDomain详解
后端·微软·c#·asp.net·.net·wpf·.netcore
武藤一雄8 天前
C#:Linq大赏
windows·后端·microsoft·c#·.net·.netcore·linq
by__csdn9 天前
第一章 (ASP.NET Core入门)第三节( 认识.NET Standard)
后端·c#·asp.net·.net·.netcore·f#·vb.net