web.config使用方法指南
程序员文章站
2024-02-13 20:15:40
<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当做配置文件,应该也可以同样实现吧!
以上所述就是本文的全部内容了,希望大家能够喜欢。