Asp.net

自定義部分的 Web 配置轉換

  • August 3, 2015

我的 MVC 5 應用程序中有許多不同的 Web.configs 用於不同的環境 - 例如測試/生產

我有網路轉換來改變不同環境的值。因此,例如,我的 web.config 文件中有以下應用程序設置:

<appSettings>
<add key="DevDisplayPanel" value="true" />
</appSettings>

然後在我的 Web.Test.config 和 Web.Prod.config 中使用 Web 轉換來更改值,如下所示:

<appSettings>
<add key="DevDisplayPanel" 
    xdt:Transform="Replace" 
    xdt:Locator="Match(key)" 
    value="false" />
<appSettings>

但是,在我的 Web.config 中,我也有自己的自定義部分,該部分位於該<appSettings>部分之外,如下所示:

 <myCustomSection>
   <serverList>
     <add zone="Zone1" url="https://dev-myurl1.com"/>
     <add zone="Zone2" url="https://dev-myurl2.com"/>
     <add zone="Zone2" url="https://dev-myurl3.com"/>
   </serverList>
 </myCustomSection>

我的問題是 - 是否可以進行 Web 轉換,以便 Test 和 Prod 如下所示:

測試:

 <myCustomSection>
   <serverList>
     <add zone="Zone1" url="https://test-myurl1.com"/>
     <add zone="Zone2" url="https://test-myurl2.com"/>
     <add zone="Zone2" url="https://test-myurl3.com"/>
   </serverList>
 </myCustomSection>

產品:

 <myCustomSection>
   <serverList>
     <add zone="Zone1" url="https://prod-myurl1.com"/>
     <add zone="Zone2" url="https://prod-myurl2.com"/>
     <add zone="Zone2" url="https://prod-myurl3.com"/>
   </serverList>
 </myCustomSection>

您可以嘗試替換<serverList>標籤的內容。

測試:

<myCustomSection>
   <serverList xdt:Transform="Replace">
       <add zone="Zone1" url="https://test-myurl1.com"/>
       <add zone="Zone2" url="https://test-myurl2.com"/>
       <add zone="Zone2" url="https://test-myurl3.com"/>
   </serverList>
</myCustomSection>

產品:

<myCustomSection>
   <serverList xdt:Transform="Replace">
       <add zone="Zone1" url="https://prod-myurl1.com"/>
       <add zone="Zone2" url="https://prod-myurl2.com"/>
       <add zone="Zone2" url="https://prod-myurl3.com"/>
   </serverList>
</myCustomSection>

引用自:https://stackoverflow.com/questions/28917289