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. 但随之产生 ...
随机推荐
- 软工作业1—java实现wc.exe
github项目地址 https://github.com/liyizhu/wc.exe WC 项目要求 基本功能列表: wc.exe -c file.c //返回文件 file.c 的字符数 ...
- go的包下载失败解决方案
包被墙的方案 1 翻啊的墙 2 gopm 3 https://github.com/golang/net 4 使用国内网站打包 5 export GOPROXY=https://goproxy.io
- Numpy安装
Python官网上的发行版是不包含Numpy模块的. 1.使用已有的发起行版本 对于许多用户,尤其是在Windows上,最简单的方法就是下载以下的Python发行版,他们包涵了所有的关键包(包括Num ...
- 一次HTTP请求响应涉及了哪些?
HTTP请求和响应步骤 TCP/IP协议 TCP三次握手 HTTP协议 HTTP请求报文 HTTP响应报文 TCP四次挥手 HTTP请求和响应步骤 以上完整表示了HTTP请求和响应的7个步骤,下面从T ...
- AX_ClassTemplate
static void main(Args args) { THK_InterfaceDataExtract THK_InterfaceDataExtract; FormRun formRun = a ...
- union: redis config
# how to save to disk # warning: how to disable, just comment this config save $second $changes
- linux上安装mysql5.7
1.下载tar包,这里使用wget从官网下载 wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2. ...
- 区域检测算法-MSERs
区域检测算法-MSERs:最大稳定极值区域 参考书籍——<图像局部不变性特征与描述>王永明.王贵锦著 MSER最大极值稳定区域的提取步骤:1.像素点排序 2.极值区域生成 3.稳定 ...
- 使用electron开发指静脉客户端遇到的问题总结
使用electron 使用nodejs 的ffi模块调用dll文件 总结1.electron 与nodejs版本不需要一致,甚至nodejs版本应该高于electron的node版本2.要安装 Vis ...
- 现网环境业务不影响,但是tomcat启动一直有error日志,ERROR org.apache.catalina.startup.ContextConfig- Unable to process Jar entry [module-info.class] from Jar [jar:file:/home/iufs/apache-tomcat/webapps/iufs/WEB-INF/lib/asm
完整的错误日志信息: 2019-03-19 15:30:42,021 [main] INFO org.apache.catalina.core.StandardEngine- Starting Ser ...