1.项目中添加App.config文件

因为.NET Core的项目本质是控制台应用,所以ConfigurationManager的API会去默认读取app.config配置文件,而不是web.config配置文件。

2.如果是asp.net迁移过来的配置文件,去除config中和需要的配置无关的内容,主要是<system.web>、 <system.webServer><system.codedom>等典型asp.net标签。

<?xml version="1.0" encoding="utf-8"?>
<configuration> <!-- To customize the asp.net core module uncomment and edit the following section.
For more info see https://go.microsoft.com/fwlink/?linkid=838655 -->
<!--
<system.webServer>
<handlers>
<remove name="aspNetCore"/>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
</system.webServer>
--> <appSettings>
<add key="Email" value="1037134@qq.com" />
</appSettings> <connectionStrings>
<add name="TestCon" connectionString="Data Source=.;Initial Catalog=OWNDB;user id=sa;pwd=123456" />
</connectionStrings> </configuration>

3.引入【 System.Configuration.ConfigurationManager 】NUGET包

4.读取

var email = System.Configuration.ConfigurationManager.AppSettings["Email"];

var conn = System.Configuration.ConfigurationManager.ConnectionStrings["TestCon"];

【Core】.NET Core中读取App.config配置文件的更多相关文章

  1. C# 读取app.config配置文件 节点键值,提示 "配置系统未能初始化" 错误的解决方案

    新建C#项目,在app.config中添加了appSettings项,运行时出现"配置系统未能初始化"的错误,MSDN里写到,如果配置文件中包含 configSections 元素 ...

  2. winform中读取App.config中数据连接字符串

    1.首先要在工程引用中导入System.Configuration.dll文件的引用. 2.通过System.Configuration.ConfigurationManager.Connection ...

  3. C# 读取app.config配置文件节点键值,提示"配置系统未能初始化" 错误的解决方案

    MSDN里写到, 如果配置文件中包含 configSections 元素,则 configSections 元素必须是 configuration 元素的第一个子元素. 将自己添加的appSettin ...

  4. 读取app.config配置文件信息

    <?xml version="1.0" encoding="utf-8" ?> <configuration> <startup& ...

  5. C# 读写App.config配置文件

    一.C#项目中添加App.config配置文件 在控制台程序中,默认会有一个App.config配置文件,如果不小心删除掉,或者其他程序需要配置文件,可以通过添加得到. 添加步骤:右键项目名称,选择“ ...

  6. C# 读App.config配置文件[2]: .Net Core框架

    C# 读App.config配置文件[1]:.Net Framework框架 C# 读App.config配置文件[2]: .Net Core框架 网上都是.net framework读取配置文件的方 ...

  7. C#中动态读写App.config配置文件

    转自:http://blog.csdn.net/taoyinzhou/article/details/1906996 app.config 修改后,如果使用cofnigurationManager立即 ...

  8. 如何在ASP.NET Core自定义中间件中读取Request.Body和Response.Body的内容?

    原文:如何在ASP.NET Core自定义中间件中读取Request.Body和Response.Body的内容? 文章名称: 如何在ASP.NET Core自定义中间件读取Request.Body和 ...

  9. [转]WinForm和WebForm下读取app.config web.config 中邮件配置的方法

    本文转自:http://blog.csdn.net/jinbinhan/article/details/1598386 1. 在WinForm下读取 App.config中的邮件配置语句如下: Con ...

随机推荐

  1. Golang自定义包导入

    # 文件Tree project -/bin -/pkg -/src -main.go -/test -test1.go -test2.go main.go package main import ( ...

  2. LeetCode 520 Detect Capital 解题报告

    题目要求 Given a word, you need to judge whether the usage of capitals in it is right or not. We define ...

  3. Spring Boot 注解配置 day03

    一.SpringBoot注解 @PropertySource 和 @ImportResource和@Bean 1.@PropertySource 加载指定位置的配置文件(只能是properties文件 ...

  4. 配置DNS Server容易忽略的问题

    1.named服务启动成功,但nslookup解析报错: [root@xiamihost3 named]# service named restart 停止 named: [确定] 启动 named: ...

  5. 使用scrapy入门教程

    创建项目 scrapy startprogect demo 创建爬虫 scrapy genspider myDomain madomian.com 直接创建文件也可以 运行爬虫 scrapy craw ...

  6. WebDriver与文件系统

    1.屏幕截屏操作:其接口函数是TakesScreenshot.该功能是在运行测试用例的过程中,需要验证某个元素的状态或者显示的数值时,可以将屏幕截取下来进行对比:或者在异常或者错误发生的时候将屏幕截取 ...

  7. (转载)intellj idea 如何设置类头注释和方法注释

    原文地址:http://www.cnblogs.com/wvqusrtg/p/5459327.html           intellj idea的强大之处就不多说了,相信每个用过它的人都会体会到, ...

  8. Unity之获取资源包的路径

    先从缓存中获取,如果获取不到,则从包中获取. 如下: public static string GetAssetBundlePath(string path) { // 先尝试从 persist 目录 ...

  9. winform中的dateTimePicker控件设置默认值为空

    winform中的dateTimePicker控件设置默认值为空   第一步:设置Format的属性值为“Custom” 第二步:设置CustomFormat的属性值为空,需要按一个空格键

  10. flask 在视图函数里操作数据库

    在视图函数里操作数据库 在视图函数里操作数据的方式和在python shell中的联系基本相同,只不过需要一些额外的工作.比如把查询结果作为参数 传入模板渲染出来,或是获取表单的字段值作为提交到数据库 ...