首页 > 移动平台 > 详细

.Net Core 全局读取配置 appsettins.json

时间:2019-12-10 19:40:58      阅读:196      评论:0      收藏:0      [点我收藏+]

创建ConfigHelper.cs

1、ConfigHelper.cs 代码如下

 1     public class ConfigHelper
 2     {
 3         private static IConfiguration Config = null; //ConfigurationRoot
 4         public static void _Init(IConfiguration Configuration)
 5         {
 6             Config = Configuration;
 7         }
 8 
 9         public static string Get(string Key)
10         {
11             IConfigurationSection ISection = Config.GetSection(Key);
12             return ISection.Value;
13         }
14 
15         public static void Set(string Key,string Value)
16         {
17             IConfigurationSection ISection = Config.GetSection(Key);
18             ISection.Value = Value;
19         }
20     }

 

配置ConfigHelper引用

1、在Startup.cs 构造函数,新增如下

1  public Startup(IConfiguration configuration)
2         {
3             Configuration = configuration;
4             ConfigHelper._Init(configuration); //配置ConfigHelper
5         }

 

测试调用

 

 1 // appsettings.json 文件如下
 2 //{
 3 //    "AppSettings": {
 4 //    "downloadFileUrl": "https://hualai-glotx",
 5 //    "localFileUrl": "D:\\mydata4vipday_en.datx",
 6 //    "RefreshTokenValidDays": 30
 7 //  }
 8 //}
 9 
10 //测试调用
11 ConfigHelper.Get("AppSettings:localFileUrl");

 

ps:如下只是将Config 静态化,方便取存。有更好的方式可以留言。。

.Net Core 全局读取配置 appsettins.json

原文:https://www.cnblogs.com/sevenmall/p/12018230.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!