Core 读取配置文件
新建控制台
static void Main(string[] args)
{
Console.WriteLine("Hello World!"); //获取应用程序的当前工作目录 包含当前工作目录路径的字符串,但不包含//以反斜杠(\)结束。
var pathToContentRoot = Directory.GetCurrentDirectory();
//用于构建基于键/值的配置设置,以便在应用程序中使用
var builder = new ConfigurationBuilder()
.SetBasePath(pathToContentRoot)//将基于文件的提供程序的FileProvider设置为PhysicalFileProvider基本路径
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)//在构建器的路径中添加JSON配置提供程序
.AddEnvironmentVariables();//添加读取的Microsoft.Extensions.Configuration.IConfigurationProvider来自环境变量的配置值
Coin.Service.Models.CoinAppSettings.CreateInstence(builder.Build());// 获取具有指定键的配置子节 这边开始读取配置文件了!!! Console.WriteLine("Hello World!");
Console.ReadKey();
}
CreateInstence 这个方法的实现 及 CoinAppSettings这个类库的代码如下
public class CoinAppSettings
{
public DbConnection ConnectionStrings { get; }
public AppSettings AppSettings { get; }
public static CoinAppSettings Instance { get; private set; }
public static void CreateInstence(IConfigurationRoot builder)
{
Instance = new CoinAppSettings(builder);
}
public CoinAppSettings(IConfigurationRoot builder)
{
this.ConnectionStrings = new DbConnection(builder.GetSection("ConnectionStrings"));
this.AppSettings = new AppSettings(builder.GetSection("AppSettings"));
}
}
public class AppSettings
{
public string ApiHost { get; }
public AppSettings(IConfigurationSection section)
{
this.ApiHost = section.GetSection("ApiHost").Value;
}
}
Core 读取配置文件的更多相关文章
- .NET Core 读取配置文件方式总结
基于.NET Core的跨平台开发,配置文件与之前.NET Framework采用xml的config文件不同,目前主要是采用json文件键值对配置方式读取. 参考网上相关资料总结如下: 一.引入扩展 ...
- .net core 读取配置文件的值
.net core中的配置文件可以存一些自定义的值,我们需要去读取 在配置中添加json: "name": "sealee", "Connection ...
- ASP .NET CORE 读取配置文件的方法
老式的config文件在ASP.net core2.0中变成了appsettings.json,如果想要读取自定义配置,可以写如下代码 { "Logging": { "I ...
- .net core 读取配置文件
/// <summary> /// 读取配置信息 /// </summary> public class Zconfig { #region 读取配置信息 /// <su ...
- .Net Core 读取配置文件 appsettings.json
1. 首先些一个类 public class MySettings { public string P1 { get; set; } public string P2 { get; set; } } ...
- Asp.net Core 和类库读取配置文件信息
Asp.net Core 和类库读取配置文件信息 看干货请移步至.net core 读取配置文件公共类 首先开一个脑洞,Asp.net core 被使用这么长时间了,但是关于配置文件(json)的读取 ...
- .net core 学习 读取配置文件
在空项目中是没有配置文件的,首先要新建一个,配置文件内容如下,下面来读取各个内容 { "ConnectionStrings": { "DefaultConnection& ...
- .net core 读取appsettings.json乱码
.net core 读取配置文件乱码:vs2019读取appsettings.json乱码问题; .net core 读取appsettings.json乱码问题;用notepad++或者其他编辑器打 ...
- 【无私分享:ASP.NET CORE 项目实战(第八章)】读取配置文件(二) 读取自定义配置文件
目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 我们在 读取配置文件(一) appsettings.json 中介绍了,如何读取appsettings.json. 但随之产生 ...
随机推荐
- skynet记录2:模块简介
稍后填坑 bson.so client.so lpeg.so md5.so skynet.so sproto.so gate.so harbor.so logger.so snlua. ...
- Saliency Detection: A Spectral Residual Approach
Saliency Detection: A Spectral Residual Approach 题目:Saliency Detection: A Spectral Residual Approach ...
- 树莓派3 开机自启动(SPI)
转自:https://www.raspberrypi-spy.co.uk/2014/08/enabling-the-spi-interface-on-the-raspberry-pi/ 方案一:图形界 ...
- ES6 Template Strings(转)
转自:https://developers.google.com/web/updates/2015/01/ES6-Template-Strings Strings in JavaScript have ...
- Apache万网SSl证书安装不成功的一个注意事项(https安全链接,基于phpstudy)
最近,微信小程序挺火,要做小程序网站域名必须要有ssl证书,形成https://的安全链接,我于是从万网上下载了一个免费的ssl证书,按照万网提供的安装方式怎么也安装不好,最后我这个Apache小白费 ...
- Ajax基本语法
案例代码: $(function(){ $('#send').click(function(){ $.ajax({ type: "GET", url: "test.jso ...
- 十分钟带你读懂《增长黑客》zz
背景 “If you are not growing, then you are dying. ”(如果企业不在增长,那么就是在衰亡!) 这句话适用于企业,也适用于个人.人生毕竟不像企业,是非成败,似 ...
- jq封装
<div id='container' class='container'>盒子</div> <button onClick="f()" >te ...
- HDU 5355 Cake (构造 + 暴力)
题意:给定 n,m,让你把 1 ~ n 分成 m 部分,而且每部分和是一样大的. 析:首先先判断不能分成的,第一种是 sum (1 ~ n 的和)不能被 m 整除,或者 sum / m < n, ...
- 【慕课网实战】六、以慕课网日志分析为例 进入大数据 Spark SQL 的世界
DataFrame它不是Spark SQL提出的,而是早起在R.Pandas语言就已经有了的. A Dataset is a distributed collection of data:分布式的 ...