標籤:
投石問:
很多時候在Web.Config中的配置在生產與Prod環境都是有所不同的 , 一大堆綠綠的注釋,debug的設定與 pord的設定混合在一起,這個你能忍?!
那麼如何更好的去管理自己項目中的 Web.Config , ok , let‘s moving .
問路答:
在編譯發布的過程中,Microsoft Visual Studio 2010 及以下版本 會以 Web.Config為基礎,而後基於編譯的模式(debug or release)對 Web.Debug.Config 和 Web.Release.Config 進行載入,最終產生對應後的設定檔
第 1 步:在Web.Debug.Config 與 Web.Rlease.Config中添加 xml 命名空間
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
第 2 步:學習使用 Locator特徵文法
2-1 Locator="Condition(XPath expression)"
<!-- 在Web.Cong中選擇其 name == oldname 或 providerName == oldprovider 的Add元素,使用此串連串元素進行替換 --> <connectionStrings> <add name="DebugConnection1" connectionString="Data Source=DebugSQLServer;Initial Catalog=DebugDb;Integrated Security=True" providerName="newprovider" xdt:Locator="Condition(@name=‘oldname‘ or @providerName=‘oldprovider‘)" xdt:Transform="Replace" /> </connectionStrings>
2-2 Locator="Match(comma-delimited list of one or more attribute names)"
<!-- 在Web.Cong中選擇其 name == DebugConnection1 的Add元素,使用此串連串元素進行替換 --> <connectionStrings> <add name="DebugConnection1" connectionString="Data Source=DebugSQLServer;Initial Catalog=DebugDb;Integrated Security=True" providerName="newprovider" xdt:Locator="Match(name)" xdt:Transform="Replace" /> </connectionStrings>
2-3 Locator="XPath(XPath expression)"
<!-- Web.Cong中選擇 XPATH == configuration/connectionStrings[@name=‘DebugConnection1‘ or @providerName=‘System.Data.SqlClient‘]的元素,使用此Add元素進行替換 --><configuration xmlns:xdt="..."> <connectionStrings> <add name="DebugConnection1" connectionString="Data Source=DebugSQLServer;Initial Catalog=DebugDb;Integrated Security=True" providerName="newprovider" xdt:Locator="XPATH(configuration/connectionStrings[@name=‘DebugConnection1‘ or @providerName=‘System.Data.SqlClient‘])" xdt:Transform="Replace" /> </connectionStrings></configuration>
以上的XPath運算式是將當前Add元素 (configuration/connectionStrings) 的隱式 XPath 條件與顯式指定的運算式組合起來的結果
注1:Locator 特性是可選的。如果未指定元素的Locator 特性,預設是這個元素將會找到第一個符合自上往下的結構的標籤名的元素執行已設定的 Transform 特性。例子如下:
<!-- 將替換整個 system.web 元素,因為未指定任何 Locator 特性來指示其他方面 --> <system.web xdt:Transform="Replace"> <customErrors defaultRedirect="GenericError.htm" mode="RemoteOnly"> <error statusCode="500" redirect="InternalError.htm"/> </customErrors></system.web>
第3步 學習使用 Transform 特徵文法
| 轉換 |
描述 |
| xdt:Transform=“Replace” |
替換第一個匹配Locator的節點元素 |
| xdt:Transform=“Remove” |
清除第一個匹配Locator的節點元素 |
| xdt:Transform=“RemoveAll” |
清除所有匹配的節點標籤名的元素 |
| xdt:Transform=“Insert” |
將指定的元素添加到同級元素的末尾 |
| xdt:Transform=“SetAttributes(attributeNames)” |
找到指定的元素,並修改對應屬性的值 |
| xdt:Transform=“RemoveAttributes(attributeNames)” |
找到指定的元素,如果有指定的屬性,清除屬性 |
| xdt:Transform=“InsertBefore(XPath)” |
在指定Xpath前插入指定元素 |
| xdt:Transform=“InsertAfter(XPath)” |
在指定Xpath後插入指定元素 |
注2:Locator 特性可置於父級元素中。那麼其子項目的集合將自動適應已設定的Transform特性,預設是 if exist replace , else add .
注3:如果指定的元素只有 Locator 特性而沒有指定任何的 Transform 特性,那麼將不會發生任何 Transform特性的操作,小心與 注1 混淆
注4:父元素上的 Transform 特性會影響其所有的子項目,即使沒有為子項目指定任何 Transform 特性也是如此 , 故盡量不要在 父元素 中設定 Transform 特性。
參考文獻:
用於 Web 應用程式項目部署的 Web.config 轉換文法
ASP.net 4.0 新特性:Web.Config Transformation
[Solution] 如何更好的管理MVC項目的Web.Config