在Web.Config文件中使用configSource

在Web.Config文件中使用configSource

我们都知道,在asp.net中修改了配置文件web.config后,会导致应用程序重启,所有会话(session)丢失。然而,应用程序的配置信息放在配置文件里是最佳选择,在后台修改了配置后导致所有会话丢失是非常不爽的事情,这个时候可将配置文件中经常需要改变的参数配置节放到外面来,例如appSetting节。

 

一、原来的web.config文件:

<?xml version="1.0" encoding="utf-8"?> 
<configuration>
  <appSettings>
    <add key="CacheTimeInfo" value="30" />
    <add key="CacheTimeNews" value="10" />
    <add key="CacheTimeProduct" value="60" />
    <add key="CacheTimeTrade" value="5" />
    <add key="SiteName" value="中国叉叉网"/>
    <add key="SiteDomain" value="chinaxx.com"/>
  </appSettings>
  <connectionStrings/>
  <system.web>
    <compilation debug="false">
    </compilation>
    <authentication mode="Windows" />
  </system.web>
</configuration>

二(1/2)、现在的web.config文件

<?xml version="1.0" encoding="utf-8"?> 
<configuration>
  <appSettings configSource="Config\AppSettings.config" />
<connectionStrings/>
  <system.web>
    <compilation debug="false">
    </compilation>
    <authentication mode="Windows" />
  </system.web>
</configuration>

二(2/2)、现在的Config目录下的AppSettings.config文件

<?xml version="1.0" encoding="utf-8"?>
<appSettings>
<add key="CacheTimeInfo" value="30" />
<add key="CacheTimeNews" value="10" />
<add key="CacheTimeProduct" value="60" />
<add key="CacheTimeTrade" value="5" />
<add key="SiteName" value="中国叉叉网"/>
<add key="SiteDomain" value="chinaxx.com"/>
</appSettings>

这样在程序中修改Config\AppSettings.config文件,就不会导致重启了。

posted @ 2011-01-16 22:24  scgw  阅读(1637)  评论(0编辑  收藏  举报