注意:本功能在.Net Core中已經不可用,暫時需手動修改web.config中的信息,或者將其設置在appsettings.XXX.json中,而後再使用web.config中的環境變量來制定使用的具體appsettings文件。html
轉自:https://www.cnblogs.com/hugogoos/p/6426887.htmlweb
調試,發佈Asp.net程序的時候,開發環境和發佈環境的Web.Config每每不一樣,好比connectionstring等。若是經常有調試,發佈的需求,就須要經常修改web.config文件,這每每是一件很是麻煩的事情。json
Web.Config Transformation可以在不一樣的發佈環境下,產生不一樣的web.config文件,很是方便和實用。服務器
在新建的Web項目中有個默認的web.config, 還能夠定義格式爲web.[name].config文件, 這個配置文件定義的規則, 在發佈的時候, 會對web.config文件進行修改。app
默認項目中, 會建立Web.Debug.config和Web.Release.config文件,分別對應於Debug和Release環境。ide
假如咱們要經常發佈到測試服務器上,測試服務器和開發時候的connectionstring是不一樣的,看看如何使用Web.Config Transformation來解決這個問題。測試
1,首先添加Test配置網站
菜單Build->Configuration Manager, 就能看到以下的配置窗口, 添加一個新的配置Test.ui
也能夠從這而進入添加spa
2,添加Test config Transformation文件
在web.confg上,點擊右鍵,Add Config Transform, VS就會爲剛剛新建的Test配置新增Transformation文件 Web.Test.config
3. 修改Web.Test.config文件
下面的Web.Test.config中可以替換web.config中的connectionstring, 關鍵是這一段
這段代碼要釋放出來的。
<add name="MyDB" connectionString="Data Source=TestSQLServer;Initial Catalog=MyTestDB;Integrated Security=True" xdt:Transform="Replace" xdt:Locator="Match(name)"/>
xdt:Transform="Replace", 指的是用來作替換操做
xdt:Locator="Match(name), 指的是匹配規則,這裏是匹配name
意思是用Web.Test.config中的這個配置節用來替換web.config中name爲MyDB的配置
4. 檢查發佈的結果
選擇在Test配置下publish網站,你能看到最終的web.config文件,已經實現了替換connection string.
到這裏已經就完成了多環境發佈配置問題了,而不須要每次發佈都要修改配置。
這只是解決了發佈時的問題,細心的同窗應該發現我上面還提到了調試。
其實有時候爲了還原bug可能要連接不一樣的環境進行測試,怎麼樣能夠在程序編譯的時候就進行選擇不一樣的配置呢?
首先找到當前項目
用記事本打開拉到最下面:
加入紅框中的這段話就能夠了。
先看看原始的配置
web.config的配置爲:
web.debug.config的配置爲:
web.Test.config的配置爲:
這時只要選擇
選擇從新生成解決方案,
選擇替換,web.config配置就會被替換爲web.test.config中的配置了,以下圖web.config的配置:
connectionString的值替換爲Test中的配置了。
<Target Name="BeforeBuild"> <TransformXml Source="Web.Debug.config" Transform="Web.$(Configuration).config" Destination="Web.config" /> </Target>
這句話的意思就是以Web.Debug.config裏面的配置爲模板,生產配置存放到web.config裏面
以下圖在Debug新加一個鍵值
在選擇Test從新生成一次
發現多了一個Debug配置的Key.
其實在編譯時支持改變配置文件會有反作用的可能會對版本控制有影響,好比,你剛改了web.config,而後你用了一個debug的配置去編譯,你的web.debug.config就會自動覆蓋web.config,而後你以前的修改就沒了。比較理想的方式是須要一個第三方的web.base.config,全部的環境配置文件,例如web.debug.config,都基於web.base.config去修改,就是以web.base.config爲模板,web.config成爲修改後的目標文件,自己是隻讀的,不容許修改,只容許生成,同時也不簽入到版本庫。
參考博客 http://www.cnblogs.com/worksguo/archive/2009/08/29/1556307.html
下面有個表,來詳細列舉locator的語法
(1)Match;
這裏你須要就是在你直接匹配的屬性名。
<connectionStrings> <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中應用有邏輯性的判斷表達式。
<connectionStrings> <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的標準
<location path="c:\MySite\Admin" > <system.web xdt:Transform="Replace" xdt:Locator="XPath(//system.web)"> </system.web> <location>
這裏你會發現,這裏能夠寫一些列的表達式。
(1) Replace
表示全部匹配的節點都是替換
<assemblies xdt:Transform="Replace"> <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /> </assemblies>
其實這裏描述文件時web.release.config,將要替換的文件時Web.config .
(2) Remove
刪除第一匹配的元素。
<assemblies xdt:Transform="Remove"> </assemblies>
(3)RemoveAll
刪除全部匹配的元素
<connectionStrings> <add xdt:Transform="RemoveAll"/> </connectionStrings>
(4)Insert
插入從父節點中插入,(authorization中插入<deny users="*" />)
<authorization> <deny users="*" xdt:Transform="Insert"/> </authorization>
(5)SetAttributes
直接設置Attributes
<compilation batch="false" xdt:Transform="SetAttributes(batch)"> </compilation>
(6)RemoveAttributes
刪除出Attributes
<compilation xdt:Transform="RemoveAttributes(debug,batch)"> </compilation>
(7)InsertAfter (XPath)
經過匹配 XPath的表達式的,找到節點,並子節點後面插入 XML
<authorization> <deny users="AName" xdt:Transform="InsertAfter(/configuration/system.web/authorization/ allow[@roles='Admins']") /> </authorization>
(8)InsertBefore (XPath)
經過匹配 XPath的表達式的,找到節點,並子節點前面插入 XML
<authorization> <allow roles=" Admins" xdt:Transform="InsertBefore(/configuration/system.web/authorization/ deny[@users='*'])" /> </authorization>
(9)XSLT (filePath)
能夠在外部定義 XSLT文件,來替換Web.cofig文件。
<appSettings xdt:Transform="XSLT(V:\MyProject\appSettings.xslt)"> </appSettings>