Adding appsettings.json to a .NET Core console app
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"));
}
Adding appsettings.json to a .NET Core console app的更多相关文章
- 011.Adding Search to an ASP.NET Core MVC app --【给程序添加搜索功能】
Adding Search to an ASP.NET Core MVC app 给程序添加搜索功能 2017-3-7 7 分钟阅读时长 作者 本文内容 1.Adding Search by genr ...
- Talking appsettings.json in Asp.Net Core
在ASP.NET Core中,默认提供了三个运行时环境变量,通过查看Hosting源代码我们可以看到,分别是Development.Staging.Production public static c ...
- Azure Kay Vault(一).NET Core Console App 获取密钥保管库中的机密信息
一,引言 Azure 密钥保管库用于存储敏感信息,例如链接字符串,密码,API 密钥等.我们无法直接从Azure 密钥库中访问机密!那么我们如何才能访问应用程序中的机密信息?比如,在我们的实际项目中, ...
- .net core 读取appsettings.json乱码
.net core 读取配置文件乱码:vs2019读取appsettings.json乱码问题; .net core 读取appsettings.json乱码问题;用notepad++或者其他编辑器打 ...
- .Net Core 实践 - 如何在控制台应用(.Net Core)使用appsettings.json配置
新建控制台应用(.Net Core)程序 添加json文件,命名为appsettings.json,设置文件属性 如果较新则复制.添加内容如下 { "MyWords" : &quo ...
- NET Core 控制台程序读 appsettings.json 、注依赖、配日志、设 IOptions
.NET Core 控制台程序没有 ASP.NET Core 的 IWebHostBuilder 与 Startup.cs ,那要读 appsettings.json.注依赖.配日志.设 IOptio ...
- ASP .NET CORE 根据环境变量支持多个 appsettings.json
0.背景 在开发项目的过程当中,生产环境与调试环境的配置肯定是不一样的.拿个最简单的例子来说,比如连接字符串这种东西,调试环境肯定是不能连接生产数据库的.在之前的话,这种情况只能说是你 COPY 两个 ...
- 循序渐进学.Net Core Web Api开发系列【6】:配置文件appsettings.json
系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.本篇概述 本篇描 ...
- ASP.NET Core appsettings.json 文件
ASP.NET Core appsettings.json 文件 在本节中,我们将讨论 ASP.NET Core 项目中appsettings.json文件的重要性. 在以前的 ASP.NET 版本中 ...
随机推荐
- 2018~第三届南宁市网络安全技术大赛~nnctf~write-up
Web 1.超简单 分值:100 类型:WEB 已解决 题目:超简单的web题 http://gxnnctf.gxsosec.cn:12311/ 代码审计 <?php $white_list ...
- Asp.Net Core 轻松学-玩转配置文件
前言 在 .NET Core 项目中,配置文件有着举足轻重的地位:与.NetFramework 不同的是,.NET Core 的配置文件都以 .json 结尾,这表示一个标准的 json 格式 ...
- 学习linux笔记(不断更新)
该文章主要记录学习Linux路上的一些命令,备查. 安装Linux系统 平时用的Mac,不想再去安装一遍双系统了,因此直接用docker安装了centos.主要步骤为到docker官网下载Stable ...
- Manacher's Algorithm(马拉车算法)
## 背景 该算法用于求字符串的最长回文子串长度. ## 参考文章 >[最长回文子串——Manacher 算法](https://segmentfault.com/a/1190000003914 ...
- Ubuntu16.04安装RealSense SR300驱动
原文链接 https://blog.csdn.net/u013401766/article/details/78472285 第一步:CMake 3.14.0 安装 1)下载cmake-3.14.1. ...
- Vue.js实现后台菜单切换
因为刚初学Vue.js,暂时遇到以下问题,待之后解决 点击父节点,怎么隐藏显示子节点 点击父节点或子节点,切换li的class="active" iframe怎么自适应高度 思路 ...
- npm --save-dev --save 的区别
我们在使用npm install 安装模块或插件的时候,有两种命令把他们写入到 package.json 文件里面去,比如: --save-dev(-D) --save(-S) 在 package.j ...
- SAP HUM 拆包之后的HU号码依旧存在
比如HU 194811210666,已经被执行了Unpack操作. HUMO看这个HU号码, 执行, 可以发现,这个HU是空的,里面什么都没有. 双击HU号码,进入HU显示界面, 系统状态竟然是PHE ...
- POST一下就知道:人生苦短,我用Python!
Python编程笔记---- 背景: “闸机端”简版程序要求: 读取扫描仪得到的userID; 向服务器发送请求进行验证: 根据返回值(True/False)决定闸机的信号. 1. 文件的读取 网上买 ...
- 开始食用grpc(之二)
开始食用grpc(之二) 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9570992.html ``` 前段时间有童鞋找我开专栏.搬家.甚至还有人找我写书的. ...