Asp.net
有沒有辦法使用 web.config 轉換進行“替換或插入”?
我正在使用下面文章中描述的 web.config 轉換,以便為不同的環境生成配置。
<http://vishaljoshi.blogspot.com/2009/03/web-deployment-webconfig-transformation_23.html>
我可以通過匹配鍵來進行“替換”轉換,例如
<add key="Environment" value="Live" xdt:Transform="Replace" xdt:Locator="Match(key)" />我可以做“插入”,例如
<add key="UseLivePaymentService" value="true" xdt:Transform="Insert" />但我真正發現有用的是 ReplaceOrInsert 轉換,因為我不能總是依賴具有/不具有特定密鑰的原始配置文件。
有沒有辦法做到這一點?
我找到了一個便宜的解決方法。如果您有很多需要“替換或插入”的元素,它並不漂亮並且不會很好地工作。
執行“刪除”,然後執行“InsertAfter|InsertBefore”。
例如,
<authorization xdt:Transform="Remove" /> <authorization xdt:Transform="InsertAfter(/configuration/system.web/authentication)"> <deny users="?"/> <allow users="*"/> </authorization>
在VS2012中配合
xdt:Transform="Remove"使用。xdt:Transform="InsertIfMissing"<authorization xdt:Transform="Remove" /> <authorization xdt:Transform="InsertIfMissing"> <deny users="?"/> <allow users="*"/> </authorization>