我的web.config转换不起作用

时间:2016-02-22 15:22:51

标签: 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>

在prod和test envoirment上预览时,它不会添加appSetting。

有谁知道问题是什么?

更新

这是我的整个测试转换:

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

2 个答案:

答案 0 :(得分:5)

您没有要匹配的密钥= 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>

答案 1 :(得分:0)

下面将ELeg设置的值转换为10:

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