老一代配置系统

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 配置文件的更多相关文章

  1. .NET Core配置文件加载与DI注入配置数据

    .NET Core配置文件 在以前.NET中配置文件都是以App.config / Web.config等XML格式的配置文件,而.NET Core中建议使用以JSON为格式的配置文件,因为使用起来更 ...

  2. net core体系-web应用程序-4net core2.0大白话带你入门-6asp.net core配置文件

    asp.net core配置文件   读取配置文件 asp.net core使用appsettings.json代替传统.net framework的web.config中的<appSettin ...

  3. [转].NET Core配置文件加载与DI注入配置数据

    本文转自:http://www.cnblogs.com/skig/p/6079187.html .NET Core配置文件 在以前.NET中配置文件都是以App.config / Web.config ...

  4. .Net Core配置文件

    .Net Core下如何管理配置文件 一.前言 根据该issues来看,System.Configuration在.net core中已经不存在了,那么取而代之的是由Microsoft.Extensi ...

  5. .Net Core配置文件介绍

    Net Core中的配置文件介绍 1 简单回顾.Net Framework配置文件 .Net Core中的配置文件操作较.Net Framework有了很大的改动.介绍.Net Core中配置文件操作 ...

  6. asp.net core配置文件

    读取配置文件 asp.net core使用appsettings.json代替传统.net framework的web.config中的<appSettings>节点.它的数据格式变成了j ...

  7. .NET 黑魔法 - asp.net core 配置文件的"对象存储"

    来,全都是干货. 我们都知道在Framework版本的mvc项目中,配置数据是通过web.config里的appSettings节点配置,我们不得不写一些读取配置文件字符串的类,比如保存在静态的变量中 ...

  8. .Net Core配置文件读取整理

    一 .配置文件说明 1.配置,主要是 指在程序中使用的一些特殊参数,并且大多数 仅在程序启动的之后指定不需要修改. 2.在以前.Net项目中配置文件主要指app.config或web.config,但 ...

  9. ASP.NET Core 配置文件(无处不在的依赖注入)

    前烟: .NET Core 中取消了以往的 XML 节点配置文件,改用了 *.json 格式. 在 Startup.cs 文件中,构造方法 build appsetting.json 文件, 本文主要 ...

  10. ASP.NET Core 配置文件

    在ASP.NET Core 中,应用程序配置数据可以使用JSON, XML 和 INI格式 和内置环境变量,命令行参数或内存中的集合. 1.如何获取和设置配置 ASP.NET Core配置系统针对以前 ...

随机推荐

  1. Ubuntu16.04 配置vnc4server

  2. Spring IOC是什么

    IOC 什么是IOC IOC: Inversion of Control(控制反转) 控制反转:将对象的创建权反转给(交给)Spring. 传统开发模式 业务和实现类过于紧密,底层业务要修改源代码 通 ...

  3. Linux静态路由配置

    配置了多块网卡后,需要指定数据包由哪块网卡发送,否则可能无法访问内网,这就要用到静态路由了. 配置静态路由有多种方式: 1.修改 /etc/rc.local 文件,这样每次重启后就会自动添加,如: e ...

  4. layer.msg如何让按钮的回调执行完毕后弹框不自动关闭

    问题出现:我点击“确定”时会验证“新手机号码”,如果验证不通过则不给该弹框关掉,但是实际操作时,不管验证怎么样,点击“确定”之后该弹框都会关掉. 之前的写法: layer.open({         ...

  5. Linux命令:ipcs/ipcrm命令

    ipcs/ipcrm命令  是linux/uinx上提供关于一些进程间通信方式的信息,包括共享内存,消息队列,信号 多进程间通信常用的技术手段包括共享内存.消息队列.信号量等等,Linux系统下自带的 ...

  6. MXNet/Gluon 中网络和参数的存取方式

    https://blog.csdn.net/caroline_wendy/article/details/80494120 Gluon是MXNet的高层封装,网络设计简单易用,与Keras类似.随着深 ...

  7. python的xpinyin模块:汉字转拼音

                            pypinyin 1.安装 pip install pypinyin 2.使用方法 >>> from pypinyin import ...

  8. Shell Script 入门教程

    和 Shell 的区别 Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁. Shell 即是一种命令语言,又是一种程序设计语言. Shell 是指一种应用程序,这个应用程序提 ...

  9. 生成model笔记

    https://github.com/yscacaca/DeepSense/tree/master/android_test这个才是真正的部署代码,跑这个代码就好. 跑python sample_mo ...

  10. Hibernate Tools插件的安装和使用

    http://ricki.iteye.com/blog/842343 http://blog.csdn.net/kinmet2010/article/details/5976869 http://ww ...