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

代码截图如下图:

相关推荐
The Future is mine1 天前
.Net Core 在Linux系统下创建服务
linux·运维·.netcore
*长铗归来*3 天前
ASP.NET Core Web API 中控制器操作的返回类型及Swagger
后端·c#·asp.net·.netcore
IDOlaoluo3 天前
VS2017 安装 .NET Core 2.2 SDK 教程(包括 dotnet-sdk-2.2.108-win-x64.exe 安装步骤)
.netcore
csdn_aspnet10 天前
使用 Entity Framework Code First 方法创建 ASP.NET Core 5.0 Web API
.netcore·webapi
小先生81210 天前
.NET Core项目中 Serilog日志文件配置
c#·.netcore
爱吃香蕉的阿豪10 天前
.NET Core 中 System.Text.Json 与 Newtonsoft.Json 深度对比:用法、性能与场景选型
数据库·json·.netcore
csdn_aspnet10 天前
ASP.NET Core 10.0 的主要变化
.netcore
csdn_aspnet13 天前
在 C# .NETCore 中使用 MongoDB(第 1 部分):驱动程序基础知识和插入文档
mongodb·.netcore
csdn_aspnet14 天前
在 C# .NETCore 中使用 MongoDB(第 3 部分):跳过、排序、限制和投影
mongodb·c#·.netcore
csdn_aspnet15 天前
在 C# .NETCore 中使用 MongoDB(第 2 部分):使用过滤子句检索文档
mongodb·c#·.netcore