首页
视频
资源
登录
原
.net core 文件配置提供程序
5920
人阅读
2020/6/19 14:03
总访问:
2594228
评论:
0
收藏:
0
手机
分类:
.net后台框架
![.netcore](https://img.tnblog.net/arcimg/hb/c857299a86d84ee7b26d181a31e58234.jpg ".netcore") >#.net core 文件配置提供程序 [TOC] <br/> 相关程序包 ------------ <br/> - Microsoft.Extensions.Configuration.Ini - Microsoft.Extensions.Configuration.Json - Microsoft.Extensions.Configuration.NewtonsoftJson - Microsoft.Extensions.Configuration.Xml - Microsoft.Extensions.Configuration.UserSecrets <br/> 特性 ------------ <br/> - 指定文件可选、必选 - 指定是否监视文件的变更 <br/> 简单运用示例 ------------ <br/> >###项目结构 ![项目结构](https://img.tnblog.net/arcimg/hb/ce9d4056afbd4fd5bcf0a8288736a2ee.png "项目结构") >###项目代码 <br/> ><font style="color:#f1c40f;font-weight:bold;">appsetting.json</font> ```json { "Key1": "Value1", "Key2": "Value2", "Key5": false, "Key6": 10 } ``` ><font style="color:#7f8c8d;font-weight:bold;">appsetting.ini</font> ``` Key10=Value10 in ini ``` ><font style="color:#2ecc71;font-weight:bold;">Program.cs</font> ```csharp var builder = new ConfigurationBuilder(); builder.AddJsonFile("appsetting.json",optional:false,reloadOnChange:true); var configurationRoot = builder.Build(); Console.WriteLine($"Key1:{configurationRoot["Key1"]}"); Console.WriteLine($"Key2:{configurationRoot["Key2"]}"); Console.WriteLine($"Key3:{configurationRoot["Key3"]}"); Console.ReadKey(); Console.WriteLine($"Key1:{configurationRoot["Key1"]}"); Console.WriteLine($"Key2:{configurationRoot["Key2"]}"); Console.WriteLine($"Key3:{configurationRoot["Key3"]}"); Console.ReadKey(); ``` >AddJsonFile方法参数介绍 | 参数名称 | 参数详情 | | ------------ | ------------ | | path | 通过相对路径去读取配置文件。 | | optional | false:当文件不存在时报错 <br/> true:相反 | | reloadOnChange | true:文件发生更新时数据也发生改变 <br/> false:不关心文件的更新 | <br/> >###Json配置解析运行 <br/> >运行前部分 ![Json配置解析运行](https://img.tnblog.net/arcimg/hb/091cdee1de4c4683bfb551dced51ed1c.png) >修改<font style="color:#f1c40f;font-weight:bold;">appsetting.json</font>配置 ```json { "Key1": "Value1", "Key2": "Value2", "Key3": "Value3", "Key5": false, "Key6": 10 } ``` >运行后部分 ![运行后部分](https://img.tnblog.net/arcimg/hb/21712c0bbe69457cbc59c65595713f69.png) <br/> > 当 **reloadOnChange** 为 **true** 的时候我们发现中途数据发生改变也会进行数据的更新 <br/> >###Ini配置解析运行 <br/> >完整代码如下: ```csharp var builder = new ConfigurationBuilder(); builder.AddJsonFile("appsetting.json",optional:false,reloadO builder.AddIniFile("appsetting.ini",optional:false,reloadOnC var configurationRoot = builder.Build(); Console.WriteLine($"Key1:{configurationRoot["Key1"]}"); Console.WriteLine($"Key2:{configurationRoot["Key2"]}"); Console.WriteLine($"Key3:{configurationRoot["Key3"]}"); Console.ReadKey(); Console.WriteLine($"Key1:{configurationRoot["Key1"]}"); Console.WriteLine($"Key2:{configurationRoot["Key2"]}"); Console.WriteLine($"Key3:{configurationRoot["Key3"]}"); Console.WriteLine($"Key10:{configurationRoot["Key10"]}"); Console.ReadKey(); ``` >运行结果 ![运行结果](https://img.tnblog.net/arcimg/hb/d5cf9c95d306477ebf3e8c1258fa021b.png) >###注意 <br/> >靠后的配置文件会覆盖之前存在的配置信息
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739
👈{{preArticle.title}}
👉{{nextArticle.title}}
评价
{{titleitem}}
{{titleitem}}
{{item.content}}
{{titleitem}}
{{titleitem}}
{{item.content}}
尘叶心繁
这一世以无限游戏为使命!
博主信息
排名
6
文章
6
粉丝
16
评论
8
文章类别
.net后台框架
168篇
linux
17篇
linux中cve
1篇
windows中cve
0篇
资源分享
10篇
Win32
3篇
前端
28篇
传说中的c
4篇
Xamarin
9篇
docker
15篇
容器编排
101篇
grpc
4篇
Go
15篇
yaml模板
1篇
理论
2篇
更多
Sqlserver
4篇
云产品
39篇
git
3篇
Unity
1篇
考证
2篇
RabbitMq
23篇
Harbor
1篇
Ansible
8篇
Jenkins
17篇
Vue
1篇
Ids4
18篇
istio
1篇
架构
2篇
网络
7篇
windbg
4篇
AI
18篇
threejs
2篇
人物
1篇
嵌入式
2篇
python
13篇
HuggingFace
8篇
pytorch
9篇
opencv
6篇
最新文章
最新评价
{{item.articleTitle}}
{{item.blogName}}
:
{{item.content}}
关于我们
ICP备案 :
渝ICP备18016597号-1
网站信息:
2018-2024
TNBLOG.NET
技术交流:
群号656732739
联系我们:
contact@tnblog.net
欢迎加群
欢迎加群交流技术