使用Web.Config Transformation配置靈活的配置文件


發布Asp.net程序的時候,開發環境和發布環境的Web.Config往往不同,比如connectionstring等。如果常常有發布的需求,就需要常常修改web.config文件,這往往是一件非常麻煩的事情。
Web.Config Transformation能夠在不同的發布環境下,產生不同的web.config文件,非常方便和實用。

閱讀目錄:

一、Web.Config Transformation

二、一個實際的例子

三、Web.Config Transformation具體語法

一. Web.Config Transformation

項目中有個默認的web.config, 還可以定義格式為web.[name].config文件, 這個配置文件定義的規則, 在發布的時候, 會對web.config文件進行修改。
默認項目中, 會創建Web.Debug.config和Web.Release.config文件,分別對應於Debug和Release環境。

blog11

 

二. 一個實際的例子

假如我們要常常發布到測試服務器上,測試服務器和開發時候的connectionstring是不同的,看看如何使用Web.Config Transformation來解決這個問題。

1. 添加Test配置

菜單Build->Configuration Manager, 就能看到如下的配置窗口, 添加一個新的配置Test.

blog1

 

2. 添加Test config Transformation文件

在web.confg上,點擊右鍵,Add Config Transform, VS就會為剛剛新建的Test配置新增Transformation文件 Web.Test.config

blog2

 

blog3

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的配置

<?xml version="1.0" encoding="utf-8"?>

<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <!--
    In the example below, the "SetAttributes" transform will change the value of
    "connectionString" to use "ReleaseSQLServer" only when the "Match" locator
    finds an attribute "name" that has a value of "MyDB".
    <connectionStrings>
      <add name="MyDB"
        connectionString="Data Source=TestSQLServer;Initial Catalog=MyTestDB;Integrated Security=True"
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    </connectionStrings>
  -->
    <connectionStrings>
      <add name="DefaultConnection"
        connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" providerName="System.Data.SqlClient"
        xdt:Transform="Replace" xdt:Locator="Match(name)"/>
    </connectionStrings>
  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
    <!--
      In the example below, the "Replace" transform will replace the entire
      <customErrors> section of your web.config file.
      Note that because there is only one customErrors section under the
      <system.web> node, there is no need to use the "xdt:Locator" attribute.
      <customErrors defaultRedirect="GenericError.htm"
        mode="RemoteOnly" xdt:Transform="Replace">
        <error statusCode="500" redirect="InternalError.htm"/>
      </customErrors>
    -->
  </system.web>
</configuration>

4. 檢查發布的結果

選擇在Test配置下publish網站,你能看到最終的web.config文件,已經實現了替換connection string.

blog5

 

三. Web.Config Transformation具體語法

參考博客 http://www.cnblogs.com/worksguo/archive/2009/08/29/1556307.html

1 :locator屬性

下面有個表,來詳細列舉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>

這里你會發現,這里可以寫一些列的表達式。

2: Transform 屬性

(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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM