專案隨著規模增長,單一 web.config 會變得龐大且難以維護;多環境 (Dev/Test/Staging/Prod) 參數差異導致配置漂移。.NET 2.0+ 提供 configSource 屬性,支援將配置區段外部化為獨立檔案,實現「化整為零、環境隔離、安全管控」。
<!-- Web.config --> <system.web> <profile configSource="profile.config" /> </system.web>
<!-- profile.config -->
<profile>
<properties>
<add name="Name" type="String" />
<add name="Age" type="Int32" />
</properties>
</profile>
執行時自動合併,對應用程式透明。
ConfigurationSection),不可拆分區段群組 (ConfigurationSectionGroup) 或元素 (Element)。<connectionStrings>、<appSettings>、<system.web/membership>、<system.web/roleManager>、<system.serviceModel> 等。web.config。connectionStrings.Dev.config、connectionStrings.Prod.config)。connectionStrings.config),或使用 配置轉換 (Web.config.transform)。aspnet_regiis -pef) 或存放於 Key Vault / 環境變數,不入版控。restartOnExternalChanges="true" (參閱 MSDN)。Core 採用 appsettings.json + 環境變數 + 命令列參數 + 使用者密鑰 等多層疊加配置系統,理念一致但實作不同,建議重新規劃而非直接移植。