如何在.Net Core 2.0 App中读取appsettings.json
This is something that strangely doesn’t seem to be that well documented and took me a while to figure out though in the end it’s pretty simple.
All that’s required is to add the following NuGet packages and an appsettings.json file.
- Microsoft.Extensions.Configuration
- Microsoft.Extensions.Configuration.FileExtensions
- Microsoft.Extensions.Configuration.Json
The appsettings.json files “Copy to Output Directory” property should also be set to “Copy if newer” so that the application is able to access it when published.

The settings are injected in the main method rather than in the startup method as with web apps but the code is essentially the same.
static void Main(string[] args)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); IConfigurationRoot configuration = builder.Build(); Console.WriteLine(configuration.GetConnectionString("Storage"));
Console.WriteLine(configuration.GetSection("ConnectionStrings:Storage").Value);
}
In the case of receiving the error “IConfigurationBuilder does not contain a definition for AddJsonFile” just rebuild the project and close and re-open Visual Studio.
如何在.Net Core 2.0 App中读取appsettings.json的更多相关文章
- .Net Core 2.0 App中读取appsettings.json
引用: Microsoft.Extensions.ConfigurationMicrosoft.Extensions.Configuration.FileExtensionsMicrosoft.Ext ...
- Asp.Net Core 进阶(一) —— 读取appsettings.json
我们以前在Asp.Net MVC中使用 System.Configuration.ConfigurationManager 来读取web.config文件.但是Asp.Net Core MVC已经没有 ...
- .NET Core 类库中读取appsettings.json
{ "Logging": { "IncludeScopes": false, "LogLevel": { "Default&quo ...
- .NET Core 中读取appsettings.json配置文件的方法
appsettings.json配置文件结构如下: { "WeChatPay": { "WeChatApp_ID": "wx9999998999&qu ...
- ASP.NET Core 3.0 WebApi中使用Swagger生成API文档简介
参考地址,官网:https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/getting-started-with-swashbuckle?view ...
- Structured Streaming从Kafka 0.8中读取数据的问题
众所周知,Structured Streaming默认支持Kafka 0.10,没有提供针对Kafka 0.8的Connector,但这对高手来说不是事儿,于是有个Hortonworks的邵大牛(前段 ...
- .NET Core类库项目中如何读取appsettings.json中的配置
这是一位朋友问我的问题,写篇随笔回答一下.有2种方法,一种叫丑陋的方法 —— IConfiguration ,一种叫优雅的方法 —— IOptions . 1)先看丑陋的方法 比如在 RedisCli ...
- .Net Core3.0 WebApi 项目框架搭建 三:读取appsettings.json
.Net Core3.0 WebApi 项目框架搭建:目录 appsettings.json 我们在写项目时往往会把一些经常变动的,可能会变动的参数写到配置文件.数据库中等可以存储数据且方便配置的地方 ...
- Asp .Net Core 读取appsettings.json配置文件
Asp .Net Core 如何读取appsettings.json配置文件?最近也有学习到如何读取配置文件的,主要是通过 IConfiguration,以及在Program中初始化完成的. ...
随机推荐
- jquery对象与核心函数
原生js入口函数与jQuery入口函数的区别 1.window.onload与document.ready在加载时间上的差异 document.ready会先触发,其次才是window.onload ...
- JVM jmap
需求:经常会因为OOM而导致系统挂掉,很多服务无法连接,所以准备了解一下. 参考:http://www.open-open.com/lib/view/open1390916852007.html 一. ...
- git管理之源切换
Git remote 修改源 git commit -m "Change repo." # 先把所有为保存的修改打包为一个commit git remote remove orig ...
- UNIX/Linux系统管理技术手册(2)----bash脚本编程
1. 一个简单的例子: $ vim readname.sh #file:readname.sh#!/bin/bash echo -n "Enter your name: " rea ...
- 基于Vue的WebApp项目开发(一)
了解webpack的魔力: 项目结构以及开发环境 webpack初体验之打包文件 1.首先创建三个文件,分别是index.html.main.js和calc.js index.html <!DO ...
- ES6学习笔记(一)
‘变量’声明 ES6新增两个声明’变量’的关键字,let和const命令. l let用来声明局部变量 同ES5中var声明的变量不同的是,let声明的变量的作用域范围仅仅是从其声明的地方开始,到其 ...
- Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context.
代码如下: var query = from s in db.LoginUserServices join ss in db.Services on s.ServiceType equals ss.C ...
- C#启动外部程序(进程)
通过调用Process类可以启动系统内部(环境变量里的)或者指定位置的程序,例如: Process.Start("notepad");//启动记事本 Process.Start(& ...
- centos7主机名的修改
在CentOS中,有三种定义的主机名:静态的(static),瞬态的(transient),和灵活的(pretty).“静态”主机名也称为内核主机名,是系统在启动时从/etc/hostname自动初始 ...
- mysql5.7
原文 http://imysql.com/2015/07/08/mysql-57-new-feature-part-3.shtml 主题 MySQL 本文将和大家一起分享下5.7的新特性,不过我们要 ...