web.config使用方法指南

  <connectionStrings configSource="db.config"/>

  外部文件db.config:

  

复制代码 代码如下:

  <connectionStrings>

  <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-WebApplication1-20140304225906;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-WebApplication1-20140304225906.mdf"

  providerName="System.Data.SqlClient" />

  <add name="ReportServerTempDBConnectionString" connectionString="Data Source=.;Initial Catalog=ReportServerTempDB;Integrated Security=True"

  providerName="System.Data.SqlClient" />

  </connectionStrings>

  具体格式

  <节点名 configSource="配置文件路径"/>

  注意点

  1.如果指定了configSource,就以外部文件为准,web.config中connectionStrings下面的节点配置就无效了,即使web.config中connectionStrings下面的连接串节点没有删除掉也是无效的。

  2.外部文件的根节点必须与web.config中指定的节点相同(其实就是将该节点的配置移到外部文件中)

  3.一个节点只能指定一个外部配置文件,不能将多个节点的外部文件指定为同一个,如不能将connectionStrings与appSettings的configSource指定为同一个文件。

  节点的数据获取

  ConfigurationManager.GetSection("节点名");//返回的是一个节点对象

  假想:架设以后ASP.NET5跨平台,使用json当做配置文件,应该也可以同样实现吧!

  以上所述就是本文的全部内容了,希望大家能够喜欢。