web.config設定檔中的configSource屬性

來源:互聯網
上載者:User

標籤:

每個網站裡都會有一個web.config檔案。修改Web.config檔案會導致IIS重啟,就是隨意的斷行符號一下也會導致重啟。微軟建議,不要將需要修改的配置內容儲存在web.config中。而是單獨放在一個config中。但是對於單獨存放的config檔案,怎麼來對其進行修改和讀取呢? 例如 可以指定 web.config 中的 appSetting 單獨放在 一個 config.config 檔案中。就是通過 configSource 屬性來指定。

原本的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>

拆分後的檔案

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

被拆分出來的config檔案 config.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.config不會導致IIS重啟。

說明:我的config.config檔案和webconfig在同一級目錄, 也可以放在不同的目錄下面, 但需要在configSource屬性裡面指明路徑,另外,我這裡叫config.config,你可以隨意改變,比如叫app.config。

注意config.config的寫法。

一旦在web.config中的appSettings節點中添加configSource,就不能再添加Add節點了。也就是說你不能在webconfig裡放一部分<add>,然後再config.config裡在放一部分<add>

修改時,訪問的節點必須存在否則會報:未將對象引用設定到對象的執行個體。

web.config設定檔中的configSource屬性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.