ASP.NET 4.7.2中xdt:Transform无法插入web.config新节点的问题
问题原因&解决方案
你的问题核心在于源web.config里没有<appSettings>节点——不管是VS预览还是测试工具,都找不到要插入<add>元素的父容器,自然没法完成转换。下面给你两种靠谱的解决办法:
方法1:在web.debug.config中先创建<appSettings>节点
修改你的web.debug.config,给<appSettings>节点加上xdt:Transform="Insert",这样如果源配置里没有这个节点,转换时会先创建它,再插入子元素:
<?xml version="1.0"?> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <xdt:Import assembly="AppHarbor.TransformTester" namespace="AppHarbor.TransformTester.Transforms"/> <configSections xdt:Transform="MergeBefore(/configuration/*)" /> <appSettings xdt:Transform="Insert"> <add key="key1" value="1" /> </appSettings> </configuration>
这种方式不需要改动源web.config,完全通过转换文件来补全节点。
方法2:在源web.config中添加空的<appSettings>节点
直接在你的web.config里加上空的<appSettings>节点,这样转换时就能找到对应的父路径了:
<?xml version="1.0"?> <configuration> <connectionStrings> <add name="foo" connectionString="value"/> </connectionStrings> <appSettings> <!-- 留空即可,转换会自动添加key1 --> </appSettings> <system.web> <customErrors mode="Off"/> </system.web> </configuration>
之后保持你的web.debug.config不变,再去预览或测试就能看到key1被成功添加了。
额外注意VS预览的小细节
确保你在VS里右键点击web.debug.config,选择「Preview Transform」,并且当前解决方案配置是Debug(不是Release),这样预览才会加载正确的转换规则。
内容的提问来源于stack exchange,提问作者Zizzipupp
相关产品推荐
相关产品推荐

