ASP.NET Core 类库中取读配置文件 appsettings.json

ASP.NET Core 类库中取读配置文件 appsettings.json

首先引用NuGet包

  1. Microsoft.Extensions.Configuration  
  2. Microsoft.Extensions.Configuration.Json  
  3. Microsoft.Extensions.DependencyInjection  
  4. Microsoft.Extensions.Options  
  5. Microsoft.Extensions.Options.ConfigurationExtensions  

 

我们先来看一下appsettings.json文件

 
  1. {  
  2.   "Logging": {  
  3.     "IncludeScopes"false,  
  4.     "Debug": {  
  5.       "LogLevel": {  
  6.         "Default""Warning"  
  7.       }  
  8.     },  
  9.     "Console": {  
  10.       "LogLevel": {  
  11.         "Default""Warning"  
  12.       }  
  13.     }  
  14.   },  
  15.   "AppSupportDatabase": {  
  16.     "ConnectionString""server=.;initial catalog=TestDB;user id=sa;password=123",  
  17.     "ProviderName""System.Data.SqlClient"  
  18.   }  
  19. }  

我们想取ProviderName怎么办呢?首先新建ConfigManager

 
  1. public class ConfigManager  
  2.   {  
  3.       public string ProviderName { getset; }  
  4.   
  5.       public string ConnectionString { getset; }  
  6.   }  

GetAppsettings方法

 
  1. public T GetAppsettings<T>(string key) where T : classnew()  
  2.  {  
  3.      string keyDir = System.IO.Directory.GetCurrentDirectory();  
  4.   
  5.      IConfiguration config = new ConfigurationBuilder()  
  6.          .SetBasePath(keyDir)  
  7.          .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })  
  8.          .Build();  
  9.      var appconfig = new ServiceCollection()  
  10.          .AddOptions()  
  11.          .Configure<T>(config.GetSection(key))  
  12.          .BuildServiceProvider()  
  13.          .GetService<IOptions<T>>()  
  14.          .Value;  
  15.      return appconfig;  
  16.  }  

调用例子

 
  1. GetAppsettings<ConfigManager>("AppSupportDatabase").ProviderName  

 

出处:https://www.studenty.cn/?p=1094

==========================================

需要先引用官方的nuget包

①:Microsoft.Extensions.Configuration

②:Microsoft.Extensions.Options.ConfigurationExtensions

用户自定义json的配置文件

在这里我用的配置文件名称是appsettings.json

配置文件内容如图所示:

在Startup类中的Startup方法中编辑代码,我先把代码贴出来吧:

var builder = new ConfigurationBuilder()

.SetBasePath(env.ContentRootPath)

.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)

.AddEnvironmentVariables();

Configuration = builder.Build();

var connString = new ConnectionStrings();

Configuration.GetSection("ConnString").Bind(connString);



在代码中ConnectionStrings类是一个Model,然后你创建的变量connString已经被实例化了。你可以访问了


作者:奥斯卡的肌肤
链接:https://www.jianshu.com/p/a13a0194ff91
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

posted on   jack_Meng  阅读(1653)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

支付宝打赏

主题色彩