博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在.Net Core 2.0 App中读取appsettings.json
阅读量:7025 次
发布时间:2019-06-28

本文共 1253 字,大约阅读时间需要 4 分钟。

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.

 

 

转载地址:http://hooxl.baihongyu.com/

你可能感兴趣的文章
react js vccode调试
查看>>
小米9.0系统机器最完美激活Xposed框架的步骤
查看>>
排序算法总结
查看>>
温故之.NET进程间通信——管道
查看>>
Vue.js搭配环境过程中遇到的坑
查看>>
hexo博客搭建
查看>>
常见Dom操作有哪些?
查看>>
学习TypeScript
查看>>
编写自定义 .NET Core 主机以从本机代码控制 .NET 运行时
查看>>
Java代码执行顺序
查看>>
674 Longest Continuous Increasing Subsequence
查看>>
html5,canvas实现自定义饼图
查看>>
什么是JPA?Java Persistence API简介
查看>>
SpringBoot2.1版本的个人应用开发框架 - 整合vue实现前后端分离
查看>>
Rxjava2源码分析之线程切换(subscribeOn、observeOn)
查看>>
SpringBoot整合Mybatis
查看>>
KNN分类器-Java实现
查看>>
从事iOS研发6年的面经——希望对你们有帮助,程序员必看!
查看>>
跨域的理解
查看>>
uni-app 打开第三方程序
查看>>