zoukankan      html  css  js  c++  java
  • 【Core】.NET Core中读取App.config配置文件

    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=".logsstdout" />
      </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"];

    未雨绸缪、富有耐心、律己严厉和不偏不倚
  • 相关阅读:
    避开unity的坑(转摘)
    独立游戏开发者创建工作室必看建议(转摘)
    程序员常去的网站
    使用Unity3D的50个技巧
    day 51 html 学习 js 学习
    day 50 Java Script 学习
    day 49 html 学习 css 学习
    day 47 html 学习 css 学习
    day 46 html 学习 列 表格,
    day45 html 初识,常见命令
  • 原文地址:https://www.cnblogs.com/chuankang/p/8780156.html
Copyright © 2011-2022 走看看