多个config应用
来源:互联网 发布:mysql的大于等于符号 编辑:程序博客网 时间:2024/05/16 08:46
为什么要配置多个Config文件呢?主要为一下原因:
其一:如果我的配置节很多的话web.config会变得很大,不易管理和查找;
其二:程序运行后对web.config的改动会引起站点的重启,如果系统管理员修改配置文件也会引起系统站点重启,这样会影响系统的运行。
综合以上问题,微软建议:
不要将需要修改的配置内容保存在web.config中。而是单独放在一个config中。但是对于单独存放的config文件,怎么来对其进行修改和读取呢?
例如 可以指定 web.config 中的 appSetting 单独放在 一个 WebAppSetting.config 文件中。通过 configSource 来指定。
<configuration>
<appSettings configSource="WebAppSetting.config"></appSettings>
</configuration>
然后在 WebAppSetting.config 文件中加入 <appSettings> 中的内容如
<appSettings>
<add key="see" value="changed by application" />
</appSettings>
当 使用WebConfigurationManager的时候就会自动将WebAppSetting.config中的内容加入到web.config中,你可以通过 WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath).AppSettings 来读取 appSettings的内容。同样也可以修改appSettings中的内容,但是由于最终修改的是WebAppSetting.config文件,所以可以避免因修改 web.config而引起的restart 。
修改代码如下:
Configuration cfg = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
AppSettingsSection appSetting = cfg.AppSettings;
//读取
string settingValue = appSetting.Settings["see"].Value;
//修改
appSetting.Settings["see"].Value = "changed by application";
cfg.Save();
web.config文件中:
<appSettings configSource="test.config"/>
test.config文件内容。
<?xml version="1.0" encoding="utf-8"?>
<add key="see" value="changed by application" />
</appSettings>
如果新的config文件存在不同的文件夹下,如何处理?
web.config文件中:
<appSettings configSource="office\\test.config"/>
test.config文件内容不变。
PS: 连接字符串的配置
web.config文件中:
<connectionStrings configSource="WebConnectionString.config"></connectionStrings>
WebConnectionString.config文件内容。
<?xml version="1.0" encoding="utf-8"?>
<connectionStrings>
<add name="GlobalEyeDataLayer.Properties.Settings.GlobalEyeConnectionString"
connectionString="Data Source=. ;Initial Catalog=GlobalEye;User ID=sa;Password=123"
providerName="System.Data.SqlClient" />
<add name="ConnectionString2"
connectionString="Data Source=. ;Initial Catalog=JZQY;User ID=sa;Password=123"
providerName="System.Data.SqlClient" />
</connectionStrings>
- 多个config应用
- 多个config应用
- 配置Struts应用使用多个struts-config.xml文件
- 配置多个struts-config.xml
- 多个web.config如何管理
- 使用多个struts-config配置文件,模块化
- 使用多个struts-config配置文件,模块化
- 使用多个struts-config配置文件,模块化
- flex blazed 配置多个remoting-config
- PKG-CONFIG的应用
- 在MyEclipse中创建和使用多个Struts-Config
- struts 使用多个配置文件 struts-config.xml
- 在MyEclipse中创建和使用多个Struts-Config
- 多个config文件导致的编译错误
- 多个.Net程序共用同一个config文件的方法
- 通过web.config可配置多个数据库连接
- 在 Web.config 文件中配置多个站点地图
- .net App.config 自定义配置 获取多个节点
- 在iOS中使用ZXing库
- 异或的性质和运算
- scanf 的总结
- 播放器缓冲界面显示加载速度
- Linux--5:文件类型和文件颜色含义 【我的操作系统是Ubuntu 12.04】
- 多个config应用
- 完全卸载oracle11g步骤
- HR 模式
- coding
- 按A-Z中国城市列表排名
- 搜索 和优化剪枝
- POJ2318(计算几何)
- 调试jquery出现的错误:SCRIPT257: 由于出现错误80020101
- 成都传智播客JDBC视频及讲师介绍