Asp.net

有沒有辦法使用 web.config 轉換進行“替換或插入”?

  • April 20, 2011

我正在使用下面文章中描述的 web.config 轉換,以便為不同的環境生成配置。

<http://vishaljoshi.blogspot.com/2009/03/web-deployment-webconfig-transformation_23.html>

我可以通過匹配鍵來進行“替換”轉換,例如

&lt;add key="Environment" value="Live" xdt:Transform="Replace" xdt:Locator="Match(key)" /&gt;

我可以做“插入”,例如

&lt;add key="UseLivePaymentService" value="true" xdt:Transform="Insert" /&gt;

但我真正發現有用的是 ReplaceOrInsert 轉換,因為我不能總是依賴具有/不具有特定密鑰的原始配置文件。

有沒有辦法做到這一點?

我找到了一個便宜的解決方法。如果您有很多需要“替換或插入”的元素,它並不漂亮並且不會很好地工作。

執行“刪除”,然後執行“InsertAfter|InsertBefore”。

例如,

&lt;authorization xdt:Transform="Remove" /&gt;
&lt;authorization xdt:Transform="InsertAfter(/configuration/system.web/authentication)"&gt;
 &lt;deny users="?"/&gt;
 &lt;allow users="*"/&gt;
&lt;/authorization&gt;

在VS2012中配合xdt:Transform="Remove"使用。xdt:Transform="InsertIfMissing"

&lt;authorization xdt:Transform="Remove" /&gt;
&lt;authorization xdt:Transform="InsertIfMissing"&gt;
 &lt;deny users="?"/&gt;
 &lt;allow users="*"/&gt;
&lt;/authorization&gt;

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