通过configSource提高web.config配置灵活性
2016-12-19 15:54
274 查看
文章是在其他地方看到的,记录下来~
很多时候我们会有这样的情况,开发环境和测试环境中的配置文件是不一样的,最明显的就是数据库连接串,
这样,每次我们发布一个测试版本,都要手动去修改一下配置文件,是不是很麻烦的说。其实利用web.config中的configSource属性,
可以很方便的为我们指定外部配置为该节点的内容。这样我们在web.config中只需要指定该配置的configSource的路径就行,
而我们再发布的时候,就不需要再修改web.config文件了。 例如,将connectionStrings指定为外部文件: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>
指定外部文件:
<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>说几个注意点:1.如果指定了configSource,就以外部文件为准,web.config中connectionStrings下面的节点配置就无效了,即使web.config中connectionStrings下面的连接串节点没有删除掉也是无效的。2.外部文件的根节点必须与web.config中指定的节点相同(其实就是将该节点的配置移到外部文件中)3.一个节点只能指定一个外部配置文件,不能将多个节点的外部文件指定为同一个,如不能将connectionStrings与appSettings的configSource指定为同一个文件。4.外部文件与web.config处在同一目录。
相关文章推荐
- 通过configSource提高web.config配置灵活性
- [asp.netMVC]通过configSource提高web.config配置灵活性
- 通过configSource提高web.config配置灵活性
- 通过configSource提高web.config配置灵活性
- 通过configSource提高web.config配置灵活性
- [asp.netMVC]通过configSource提高web.config配置灵活性
- ASP.NET通过配置Web.config文件提高手动绑定数据库效率的一个简单方法,不用每次手动连接数据库地址,或者数据库地址变更时只改一处即可。
- 通过继承ConfigurationSection,在web.config中增加自定义配置
- 通过web.config文件配置项目中文件夹的访问权限
- c#与vb.net在App_Code里面编译要通过,需要以下web.config的配置
- asp.net项目中通过Web.config配置文件及文件夹的访问权限!
- 在web.xml中通过contextConfigLocation配置spring
- 在web.config里使用configSource分隔各类配置
- 在R12下加载Java Bean,配置FORMS_WEB_CONFIG_FILE文件/通过AutoConfig实现Form Server配置文件的修改
- web.xml 通过contextConfigLocation配置spring 的方式
- 在web.xml中通过contextConfigLocation配置spring
- 在web.config里使用configSource分隔各类配置
- asp.net项目中通过Web.config配置文件及文件夹的访问权限!
- ext.net中通过webconfig配置设置默认样式
- 通过继承ConfigurationSection,在web.config中增加自定义配置