在asp.net 2.0中的web.config檔案中調用外部檔案

來源:互聯網
上載者:User

      在一個工作項目或者工作小組中,有可能經常要轉換工作的調試環境,比如開發環境,測試環境,部署環境,這樣有可能要對web.config檔案進行修改或改動,比如要改資料庫的連接字串,角色配置,安全配置環境等,一不小心,很容易會發生遺漏等錯誤.在asp.net 2.0的web.config檔案中,新加入了可以引入外部檔案的新特性,

  使到我們可以先預先搞好幾個檔案,比如將經常要改動的部分,如資料庫連接串部分等,按不同的開發環境,分別寫成若干個xml檔案,然後在web.config中把它們按需要調入進來.比如

  我們先建立兩個目錄,一個叫test,一個叫developer,分別存放測試和開發時,用到的不同環境,比如

  在devloper檔案中建立一個developerconnectionstring.xml,內容如下

<connectionStrings>
<add name="connstr" connectionString=
"data source=.sqlexpress;initial catalog=
northwind;integrated security=true"
providerName="System.Data.SqlClient"/>
</connectionStrings>

  再建立一個developerappsetingstring.xml如下<appSettings>

  <add key="autoemail" value="abc@abc.com /> </appSettings>

  再建立一個developermembership.xml如下

<membership defaultProvider="Northwind">
<providers>
<add name="Northwind"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="connstr"/>
</providers>
</membership>

同樣道理,可以在test目錄下,也建立相關類似的xml檔案,然後,在web.config中,可以這樣調用了

<?xml version="1.0"?>
<configuration>
<appSettings configSource="developerdeveloperappsetingstring.xml"/>
<connectionStrings
configSource="developerdeveloperconnectionstring.xml" />
<system.web>
<membership
configSource="developerdevelopermembership.xml"/>
<compilation debug="true"/>
<authentication mode="Forms"/>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</configuration>

  可以看到,在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.