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

  1. .Net Core 2.0 App中读取appsettings.json

    引用: Microsoft.Extensions.ConfigurationMicrosoft.Extensions.Configuration.FileExtensionsMicrosoft.Ext ...

  2. Asp.Net Core 进阶(一) —— 读取appsettings.json

    我们以前在Asp.Net MVC中使用 System.Configuration.ConfigurationManager 来读取web.config文件.但是Asp.Net Core MVC已经没有 ...

  3. .NET Core 类库中读取appsettings.json

    { "Logging": { "IncludeScopes": false, "LogLevel": { "Default&quo ...

  4. .NET Core 中读取appsettings.json配置文件的方法

    appsettings.json配置文件结构如下: { "WeChatPay": { "WeChatApp_ID": "wx9999998999&qu ...

  5. ASP.NET Core 3.0 WebApi中使用Swagger生成API文档简介

    参考地址,官网:https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/getting-started-with-swashbuckle?view ...

  6. Structured Streaming从Kafka 0.8中读取数据的问题

    众所周知,Structured Streaming默认支持Kafka 0.10,没有提供针对Kafka 0.8的Connector,但这对高手来说不是事儿,于是有个Hortonworks的邵大牛(前段 ...

  7. .NET Core类库项目中如何读取appsettings.json中的配置

    这是一位朋友问我的问题,写篇随笔回答一下.有2种方法,一种叫丑陋的方法 —— IConfiguration ,一种叫优雅的方法 —— IOptions . 1)先看丑陋的方法 比如在 RedisCli ...

  8. .Net Core3.0 WebApi 项目框架搭建 三:读取appsettings.json

    .Net Core3.0 WebApi 项目框架搭建:目录 appsettings.json 我们在写项目时往往会把一些经常变动的,可能会变动的参数写到配置文件.数据库中等可以存储数据且方便配置的地方 ...

  9. Asp .Net Core 读取appsettings.json配置文件

         Asp .Net Core 如何读取appsettings.json配置文件?最近也有学习到如何读取配置文件的,主要是通过 IConfiguration,以及在Program中初始化完成的. ...

随机推荐

  1. webapi 开启gzip压缩

    1.nuget安装Microsoft.AspNet.WebApi.Extensions.Compression.Server 2.global.asax.cs里引用System.Net.Http.Ex ...

  2. Linux下mongodb

    Linux下mongodb安装: 新建mongodb文件夹 下载安装包 curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3. ...

  3. Javascript 多物体运动1

    多物体运动 <!DOCTYPE html><html> <head>  <meta charset="UTF-8">  <ti ...

  4. EMSAscript

    1.javaScript 中const.var.let区别 const 定义的变量不可修改 而且必须初始化 =>解决闭包变量污染问题 var 定义的变量可以修改 如果不初始化则默认值为undef ...

  5. 使用spring tool suite(STS)工具创建spring boot项目和出现错误后的处理

    一.先下载配置maven环境 1.下载地址:http://maven.apache.org/download.cgi windows下下载zip文件 2.解压后放到某个文件目录下 3.配置环境变量 ( ...

  6. Hush Framework框架配置(转)

    在写这篇文章的时候,楼主已经饿的不行了,因为我从3点开始就在折腾Hush Framework,走了很多弯路,打铁要趁热,先把基本的过程记录下来,留待以后翻阅,同时记录其中容易走弯路的地方,特别是对于一 ...

  7. Django后台注册

  8. 如何修改settings.xml的镜像

    今天在下载一个maven依赖包时候,下载后一直显示的是upload的jar包,然后检查了下自己的settings.xml配置,发现依赖的还是以前的内网nexus得配置.所以下载不下来,现在改为ali的 ...

  9. freess(未测试)

    freess 使用 nodejs 配合 shadowsocks-windows 实现FQ (windows) 使用方法: 如果你没有安装nodejs请先安装,访问 https://nodejs.org ...

  10. C++ Notes 1 - size_type - Accelerated Ch3

    1. 为什么用string::size_type而不是int? --Why use string::size_type ? int is supposed to work! it holds numb ...