專案常在開發/測試/正式環境切換,連接字串、角色、安全設定頻繁變動。ASP.NET 2.0 `configSource` 屬性讓 `web.config` 保持乾淨,環境差異抽離到外部 XML 檔。
/Config/Developer/connectionStrings.xml
/Config/Developer/appSettings.xml
/Config/Developer/membership.xml
/Config/Test/connectionStrings.xml
...
<!-- connectionStrings.xml -->
<connectionStrings>
<add name="Conn" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=DevDB;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
<!-- appSettings.xml -->
<appSettings>
<add key="AdminEmail" value="admin@dev.local" />
</appSettings>
<!-- membership.xml -->
<membership defaultProvider="DevProvider">
<providers>
<add name="DevProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="Conn" />
</providers>
</membership>
<configuration>
<appSettings configSource="Config/Developer/appSettings.xml" />
<connectionStrings configSource="Config/Developer/connectionStrings.xml" />
<system.web>
<membership configSource="Config/Developer/membership.xml" />
<compilation debug="true" />
<authentication mode="Forms" />
</system.web>
</configuration>
修改 `web.config` 中的路徑前綴(`Developer` → `Test`),或透過建置事件/部署腳本自動替換,實現**零代碼變更**切換環境。
此特性讓配置管理更符合「配置與代碼分離」原則,極大降低部署出錯風險。