ASP.NET 如何設定測試與正式環境的 Web.Config (Debug.Config Release.Config)


  1. 說明
    1. ConnectionString By Configuration
    2. Custom Properties By Configuration
    3. pubxml 的檔案位置
  2. 參考資料
  3. 相關連結

筆記 ASP.NET 如何設定設定測試環境(Debug.Config)與正式環境(Release.Config)的 Web.Config,讓開發的過程不用手忙腳亂的手動切換不同的 ConnectionString。

logo

說明

藉由 Visual Studio 的組態管理員,可以設定多個環境配置組態,並且能夠有對應組態與 Web.config。首先由組態管理員新增組態後,再從 Web.config 「新增設定轉換」即可以產生一組新的 Web.config。

配合發佈專案的選項,可以達到一份解決方案程式碼,達到正式區、測試區、開發環境管理的需求。

ConnectionString By Configuration

Web.Release.config

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <connectionStrings>
    <add name="ConnectionName"
      connectionString="正式環境的ConnectionString"
      xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
  </connectionStrings>
</configuration>

Custom Properties By Configuration

Web.Release.config

<appSettings>
  <add key="SystemEnv" value="Prod" 
  xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
</appSettings>

ASP.NET 從 Web.Config 讀取 Property 的方式

pubxml 的檔案位置

ProjectPath\Properties\PublishProfiles

參考資料

目前僅能

如何在 debug 時使用對應組態設定的 web.config

相關連結

ASP.NET MVC 從無到有打造一個應用系統

Visual Studio 入門教學