我的 web.config 转换不起作用
My web.config transform doesn't work
我在生产环境中使用 web.config 转换来更改 link,但是在预览时,转换没有发生。
这是我的代码:
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环境中预览时,没有添加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>
下面会将 ELeg 设置的值转换为 10:
<add xdt:Transform="Replace" xdt:Locator="Match(key)" key="ELeg" value="10" />
您没有要匹配的密钥=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>
我在生产环境中使用 web.config 转换来更改 link,但是在预览时,转换没有发生。
这是我的代码:
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环境中预览时,没有添加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>
下面会将 ELeg 设置的值转换为 10:
<add xdt:Transform="Replace" xdt:Locator="Match(key)" key="ELeg" value="10" />
您没有要匹配的密钥=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>