2016-02-22 78 views
0

我使用一個web.config轉換來改變生產環境中的一個鏈接,但預覽時,轉換不會發生..我的web.config變換不起作用

這裏是我的代碼:

web.Test.config

<configuration> 
    <appSettings> 
     <add key ="ELeg" value ="1"/> 
    </appSettings> 
    </configuration> 
    <system.web> 

web.Prod.config

<configuration> 
    <appSettings> 
     <add key ="ELeg" value="10" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/> 
    </appSettings> 
    </configuration> 

當previewi在產品和測試環境中,它不會添加appSetting。

有誰知道問題是什麼?

UPDATE

這裏是我的整個測試變換:

<?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=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" 
     xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/> 
    </connectionStrings> 
    --> 

    <configuration> 
    <appSettings> 
     <add key ="ELeg" value ="1" xdt:Transform="Insert" /> 
    </appSettings> 
    </configuration> 
    <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

你不有一個密鑰= 10匹配,你有一個密鑰= ELeg;

換句話說,這個(未經測試的)轉換應該更好;

<?xml version="1.0"?> 
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> 
    <appSettings> 
    <add key ="ELeg" value ="10" xdt:Transform="SetAttributes(value)" xdt:Locator="Match(key)"/> 
    </appSettings> 
</configuration> 

變換屬性

xdt:Locator="Match(key)"    // Find elements with matching `key`. 
xdt:Transform="SetAttributes(value)" // Set the `value` attribute on the matches 

編輯的說明:如果要插入它,它甚至不存在,那麼一個普通的xdt:Transform="Insert"是你想要的東西;

<?xml version="1.0"?> 
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> 
    <appSettings> 
    <add key ="ELeg" value ="10" xdt:Transform="Insert" /> 
    </appSettings> 
</configuration> 
+0

謝謝。但是我意識到問題不在於它顯示爲與測試web.config相同的值。問題是它根本不顯示appSettings。我認爲最好有一個'xdt:Transform =「Insert(value)」'而不是'xdt:Transform =「SetAttributes(value)」'.becase我需要添加appSetting而不是替換舊的(原始web.config中沒有appSetting)我是否正確? –

+0

@ Simon.S添加了一個示例(如果我正確理解您的問題) –

+0

謝謝..我得到錯誤:「源文檔中沒有元素匹配'/ configuration/configuration/appSettings' –

0

下面將改造ELEG的設定值10:

<add xdt:Transform="Replace" xdt:Locator="Match(key)" key="ELeg" value="10" />

+0

是的,對不起,我的壞在那裏。我用隨機字母替換了我的實際值,以舉例說明它的外觀。請看我更新的問題。 –