.NET Core 配置文件
老一代配置系统
1,XML格式 格式单一。
2,配置信息Kye,只能一维化配置
3,框架信息和应用程序信息混合到一起
应用程序中是 App.config
web项目中 web.config
使用程序集 System.Configuration
新一代配置系统
1支持配置文件格式 json xml init 环境变量 memory
2参数热加载
3参数多维
nuget包
Microsoft.Extensions.Configuration
主要是用上面的
下面的是对应不同文件类型的扩展 都是扩展方法
Microsoft.Extensions.Configuration.Json
Microsoft.Extensions.Configuration.Xml
Microsoft.Extensions.Configuration.Init
Microsoft.Extensions.Configuration.EnvironmentVariables
下面的是进行模型转换的
Microsoft.Extensions.Configuration.Binder
数据读取
//1 : 运算符 层级管理 数据的话用索引
var dcf1 = config["mysql:host"];
Console.WriteLine(dcf1);
var dcf11 = config["shopidlist:1:entid"];
Console.WriteLine(dcf11); //2 getsection getsection("")[""]
var dcf2 = config.GetSection("mysql").GetSection("host").Value;
Console.WriteLine(dcf2);
var dcf21 = config.GetSection("mysql:host").Value;
Console.WriteLine(dcf21);
var dcf3 = config.GetSection("mysql")["host"];
Console.WriteLine(dcf3);
var dcf4 = config.GetSection("shopidlist").GetSection("")["entid"];
Console.WriteLine(dcf4);
var dcf5 = config.GetSection("shopidlist").GetSection("").GetSection("entid").Value;
Console.WriteLine(dcf5);
//3强类型读取 Microsoft.Extensions.Configuration.Binder
//开发中常用强类型
var dcf6 = config.GetValue<int>("shopidlist:1:entid");
Console.WriteLine(dcf6); Rootobject dcf7 = new Rootobject();
config.Bind(dcf7);
Console.WriteLine(dcf7.mysql.host);
弱类型方式读取
<1>: ':'运算符 mysql:host
IConfiguration configuration = new ConfigurationBuilder().SetBasePath(Environment.CurrentDirectory)
.AddJsonFile($"appsettings.json", optional: true, reloadOnChange: true)
//.AddXmlFile("appsettings.xml")
//.AddEnvironmentVariables()
.Build();
var info = configuration["shopidlist:0:entid"];
{
"mysql": {
"host": "192.168.23.1",
"port": 3306
},
"shopidlist": [
{ "entid": 20 },
{ "entid": 25 }
]
}
<2> GetSection (不同的人,不同的使用习惯) getSection("mysql")["host"]
//var info = configuration.GetSection("shopidlist").GetSection("1").GetSection("entid").Value;
var info = configuration.GetSection("shopidlist").GetSection("1")["entid"];
public IConfigurationSection GetSection(string key)
{
string[] textArray1 = new string[] { this.Path, key };
return this._root.GetSection(ConfigurationPath.Combine(textArray1));
}
强类型方式读取 : Microsoft.Extensions.Configuration.Binder
<1> GetValue
var info = configuration.GetValue<int>("mysql:port");
public static object GetValue(this IConfiguration configuration, Type type, string key, object defaultValue)
{
string str = configuration.GetSection(key).Value;
if (str != null)
{
return ConvertValue(type, str);
}
return defaultValue;
}
<2> 配置映射为实体类Bind,Get<T>
Rootobject rootobject = new Rootobject();
configuration.Bind(rootobject);
Configuration.GetSection("Position").Get<PositionOptions>();
StartUp类中 public void ConfigureServices(IServiceCollection services)
{
services.Configure<PositionOptions>(Configuration.GetSection("Position"));
services.AddRazorPages();
} 其他类注入
public class Test2Model : PageModel
{
private readonly PositionOptions _options; public Test2Model(IOptions<PositionOptions> options)
{
_options = options.Value;
} public ContentResult OnGet()
{
return Content($"Title: {_options.Title} \n" +
$"Name: {_options.Name}");
}
}
.NET Core 配置文件的更多相关文章
- .NET Core配置文件加载与DI注入配置数据
.NET Core配置文件 在以前.NET中配置文件都是以App.config / Web.config等XML格式的配置文件,而.NET Core中建议使用以JSON为格式的配置文件,因为使用起来更 ...
- net core体系-web应用程序-4net core2.0大白话带你入门-6asp.net core配置文件
asp.net core配置文件 读取配置文件 asp.net core使用appsettings.json代替传统.net framework的web.config中的<appSettin ...
- [转].NET Core配置文件加载与DI注入配置数据
本文转自:http://www.cnblogs.com/skig/p/6079187.html .NET Core配置文件 在以前.NET中配置文件都是以App.config / Web.config ...
- .Net Core配置文件
.Net Core下如何管理配置文件 一.前言 根据该issues来看,System.Configuration在.net core中已经不存在了,那么取而代之的是由Microsoft.Extensi ...
- .Net Core配置文件介绍
Net Core中的配置文件介绍 1 简单回顾.Net Framework配置文件 .Net Core中的配置文件操作较.Net Framework有了很大的改动.介绍.Net Core中配置文件操作 ...
- asp.net core配置文件
读取配置文件 asp.net core使用appsettings.json代替传统.net framework的web.config中的<appSettings>节点.它的数据格式变成了j ...
- .NET 黑魔法 - asp.net core 配置文件的"对象存储"
来,全都是干货. 我们都知道在Framework版本的mvc项目中,配置数据是通过web.config里的appSettings节点配置,我们不得不写一些读取配置文件字符串的类,比如保存在静态的变量中 ...
- .Net Core配置文件读取整理
一 .配置文件说明 1.配置,主要是 指在程序中使用的一些特殊参数,并且大多数 仅在程序启动的之后指定不需要修改. 2.在以前.Net项目中配置文件主要指app.config或web.config,但 ...
- ASP.NET Core 配置文件(无处不在的依赖注入)
前烟: .NET Core 中取消了以往的 XML 节点配置文件,改用了 *.json 格式. 在 Startup.cs 文件中,构造方法 build appsetting.json 文件, 本文主要 ...
- ASP.NET Core 配置文件
在ASP.NET Core 中,应用程序配置数据可以使用JSON, XML 和 INI格式 和内置环境变量,命令行参数或内存中的集合. 1.如何获取和设置配置 ASP.NET Core配置系统针对以前 ...
随机推荐
- Pytest权威教程21-API参考-03-夹具(Fixtures)
目录 夹具(Fixtures) @ pytest.fixture config.cache的 capsys capsysbinary capfd capfdbinary doctest_namespa ...
- Quartz.NET浅谈一 : 简单Job使用(定时发送QQ邮件)
Quartz.NET是一个全功能的开源作业调度系统,可用于从最小的应用程序到大型企业系统. 直接上代码吧... 一.新建一个控制台项目 略过 二.安装Nuget包 三.创建发送邮箱辅助工具类 stat ...
- [luogu 3773][CTSC 2017]吉夫特
传送门 Solution 输入一个长度为n的数列,求有多少个长度大等于2的不上升子序列满足: \[\prod_{i=2}^{k} C(a_{b_{i-1}},a_{b_i}) mod\ 2 > ...
- 模板 - 数据结构 - ST表/SparseTable
SparseTable,俗称ST表,其功能,就是静态的RMQ(区间最值查询)问题的解决.注意传入查询的时候两个参数的合法性,或者可以进行一次全部初始化来使得越界值不产生负面影响.不过访问越界是写程序的 ...
- nginx压力测试和并发预估
一.Nginx并发预估 预估算法:{(?G)*1024-system}/请求大小 (?G):表示内存大小1024:表示内存容量标准进制system:表示系统和服务占用的额外内存和需要预留的内存请求大小 ...
- 请你谈谈Cookie的弊端
a. 每个特定的域名下最多生成的cookie个数有限制 b. IE和Opera 会清理近期最少使用的cookie,Firefox会随机清理cookie c. cookie的最大大约为4096字节,为了 ...
- locust性能测试脚本模板
locust性能测试脚本模板 #!/usr/bin/env python # -*- coding: utf-8 -*- import time from locust import HttpLocu ...
- vue-使用keepAlive对上下拉刷新列表数据 和 滚动位置细节进行处理
[前言] 使用vue处理项目中遇到列表页面时,有一些细节需要注意,这里总结一下,以便优化以后的代码.如下: 1. 使用mint-ui中的LoadMore组件上下拉刷新时,有时无法触发上拉加载更多的方法 ...
- Kotlin集合——List集合
Kotlin集合——List集合 转 https://www.jianshu.com/p/3f3bb4943638 List集合的最大特征就是集合元素都有对应的顺序索引.List集合允许使用重复元 ...
- 解决 service iptables save 报错 please try to use systemctl
本文档根据 service iptables save 报错 please try to use systemctl 提供解决方案.报错 [root@Jaking ~]# service iptabl ...