ASP.NET 4.0 新特性--Web.Config Transformation(原創)
Web.Config Transformation詳解,這部分內容比較簡單,關鍵是用沒有用過的問題,所以這里希望幫助大家實踐一下。
一 概述:
在VS2010中引入在Config 文件中使用XML DOCUMENT TRANSFORM,這一個特性就是幫助你Web.config能方便的從部署配置文件轉化到產品配置文件。協助Web.Config Transformation這兩個功能就是web.debug.config, web.release.config等.這些文件的最后都會匹配到MSBuild中的配置文件去。
其實是在web.debug.config和web.release.config來寫一些描述文件,再通過Transformation Engine來轉化。
在Transformation Engine 執行下面任務
首先是識別Locator屬性是否設置,來判斷是否使用XML轉換,接著就是從原配置的XML文件中獲得相應節點,再從轉換的XML文件中招到適合Transform的值相匹配的節點,然后將他們轉化到指定的XML配置文件。在轉化中主要依賴的是Transform的attribute.
二:實踐
基礎部分:
要使用XML-Document-Transform engine就要先引用XML-Document-Transform 命名空間,如果在你的Conifg文件中引用這個命名空間,你就能在本Web.config中使用轉換描述。
要使用XML-Document-Transform engine就要先引用XML-Document-Transform 命名空間,如果在你的Conifg文件中引用這個命名空間,你就能在本Web.config中使用轉換描述。
下面是在web.release.config中使用
接下來是使用Locator,Locator是代表一組表達式,主要是基于 XPath的,通過配置Locator來查找Web.Config.并做相應的事情。
1 :locator屬性
下面有個表,來詳細列舉locator的語法
(1)Match;
這里你需要就是在你直接匹配的屬性名。
<add name="Northwind" connectionString="connection string detail"
providerName="System.Data.SqlClient"
xdt:Transform="Replace"
xdt:Locator="Match(name)" />
</connectionStrings>
Engine會再你的Web.config中找到匹配name為Norhwind的就用上面的配置文件圖替換。
(2)Condition
基于XPath,在Locator中應用有邏輯性的判斷表達式。
<add name="Northwind"
connectionString="connection string detail"
providerName="System.Data.SqlClient"
xdt:Transform="Replace"
xdt:Locator="Condition(@name=’Northwind or @providerName=' System.Data.SqlClient')" />
</connectionStrings>
上面就是Name屬性匹配‘Norhwind’的或providerName匹配System.Data.SqlClient的配置文件節點都會被替換。
(3)XPath
這個就是直接寫XPath,http://www.w3.org/TR/xpath,這里是XPath的標準
<system.web xdt:Transform="Replace" xdt:Locator="XPath(//system.web)">
</system.web>
<location>
這里你會發現,這里可以寫一些列的表達式。
2: Transform 屬性
(1) Replace
表示所有匹配的節點都是替換
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
</assemblies>
其實這里描述文件時web.release.config,將要替換的文件時Web.config .
(2) Remove
刪除第一匹配的元素。
</assemblies>
(3)RemoveAll
刪除所有匹配的元素
<add xdt:Transform="RemoveAll"/>
</connectionStrings>
(4)Insert
插入從父節點中插入,(authorization中插入<deny users="*" />)
<deny users="*" xdt:Transform="Insert"/>
</authorization>
(5)SetAttributes
直接設置Attributes
batch="false"
xdt:Transform="SetAttributes(batch)">
</compilation>
(6)RemoveAttributes
刪除出Attributes
</compilation>
(7)InsertAfter (XPath)
通過匹配 XPath的表達式的,找到節點,并子節點后面插入 XML
<deny users="AName" xdt:Transform="InsertAfter(/configuration/system.web/authorization/ allow[@roles='Admins']") />
</authorization>
(8)InsertBefore (XPath)
通過匹配 XPath的表達式的,找到節點,并子節點前面插入 XML
<allow roles=" Admins" xdt:Transform="InsertBefore(/configuration/system.web/authorization/ deny[@users='*'])" />
</authorization>
(9)XSLT (filePath)
可以在外部定義 XSLT文件,來替換Web.cofig文件。
</appSettings>
實踐;
(1)在VS2010中創建一新的asp.web Application項目中,
(2)在configuration 這設置中選中Configuration mannager,新建一個解決方案配置文件,名為Staging,并輸入節點原素。
.png)
這里如果你不創建新的Config,你可以使用默認的Web.config.
(3)在Solution Explorer中創建Web.Staging.config 。

這個就是我之前提到的描述文件,在里面寫描述通過XML Docuemnt Transform,官方也成 Transform file.
現在在你的ConnnectStrings中 添加一個ConnectString元素。
<add name="developmentDB" connectionString="Server=DevBox; Database=development; User Id=<user>; password=<password>" providerName="System.Data.SqlClient" />
</connectionStrings>
其中<user>是代表零時的標記。
(4)應用Transform和 Locator屬性,當讓你首先要引用XML-Document-Transform命名空間,
<add name="personalDB" connectionString="Server=DevBox; Database=personal; User Id=admin; password=PersonalPassword"
providerName="System.Data.SqlClient" xdt:Transform="Replace" xdt:Locator="Match(name)" />
</connectionStrings>
之后,你要在你的Solution Explorer上選擇并創建一個Package,
再將包重新使用 VS打開:
這樣Tranformation engine就會執行,你就可以向上面基礎部分說的那些特性一點一點測試。
(5)關閉使用XML-Document-Tranformation engine,你可以在Staging.config中創建一個<UseTransforms >元素
總結(Summarize)
ASP.NET 4.0中這個特性,主要能幫助大家的應用程序中的配置文件能從Debug平緩的轉換到發布配置文件,而所以想的要從開發到產品的發布的轉化,這只是其中的一部分,總的看這里主要就寫一個描述文件,而描述文件如何和目標文件關聯的,就是使用的Transform和Locator這兩個屬性來控制,再通過XML-Document-Tranformation engine來將他們粘合在一起。
Transform和Locator都做下面這些事情:
Transform :
Replacing a node
Inserting a node
Delete a node
Removing Attributes
Setting Attributes
Locator;
Match on value of a node’s attribute
Exact XPath of where to find a node
A condition match to find a node
最后別忘記, 他們是xdt擴展元素: xdt:Transform
參考
www.msdn.com
http://weblogs.asp.net/gunnarpeipman/archive/2009/06/16/visual-studio-2010-web-config-transforms.aspx
http://weblogs.asp.net/gunnarpeipman/archive/2009/06/16/visual-studio-2010-web-config-transforms.aspx
希望大家能有幫助!請多指點。
worksguo
www.rzrgm.cn/worksguo
浙公網安備 33010602011771號