有什么方法可以执行“替换或插入”操作吗?使用 web.config 转换?
我正在使用 web.config 转换(如下文所述),以便为不同环境生成配置。
http://vishaljoshi.blogspot.com/2009/03/web-deployment-webconfig- conversion_23.html
我可以通过匹配键来执行“替换”转换,例如
<add key="Environment" value="Live" xdt:Transform="Replace" xdt:Locator="Match(key)" />
,我可以执行“插入”,例如,
<add key="UseLivePaymentService" value="true" xdt:Transform="Insert" />
但是我真正发现有用的是 ReplaceOrInsert 转换,如我不能总是依赖原始配置文件有/没有某个密钥。
有什么办法可以做到这一点吗?
I'm using web.config transformation as described in the below post in order to generate configs for different environments.
http://vishaljoshi.blogspot.com/2009/03/web-deployment-webconfig-transformation_23.html
I can do a "Replace" transformation by matching on the key, e.g.
<add key="Environment" value="Live" xdt:Transform="Replace" xdt:Locator="Match(key)" />
And I can do "Inserts" e.g.
<add key="UseLivePaymentService" value="true" xdt:Transform="Insert" />
But what I would really find useful is a ReplaceOrInsert transformation, as I can't always rely on the original config file having/not having a certain key.
Is there any way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 VS2012 中与
xdt:Transform="Remove"
结合使用xdt:Transform="InsertIfMissing"
。In conjunction with
xdt:Transform="Remove"
usexdt:Transform="InsertIfMissing"
in VS2012.我找到了一个廉价的解决方法。如果您有很多需要“替换或插入”的元素,它并不漂亮,也不会很好地工作。
执行“删除”,然后执行“InsertAfter|InsertBefore”。
例如,
I found a cheap workaround. It ain't pretty and won't work very well if you have a lot of elements that needs to be "Replace Or Insert".
Do a "Remove" and then an "InsertAfter|InsertBefore".
For example,
使用
InsertIfMissing
转换确保 appSetting 存在。然后使用
Replace
转换来设置其值。您还可以使用
SetAttributes
转换而不是Replace
。不同之处在于SetAttributes
不触及子节点。这些技术比删除+插入要好得多,因为现有节点不会移动到其父节点的底部。新节点附加在末尾。现有节点保留在源文件中的位置。
此答案仅适用于较新版本的 Visual Studio(2012 或更高版本)。
Use the
InsertIfMissing
transformation to ensure that the appSetting exists.Then use the
Replace
transformation to set its value.You could also use the
SetAttributes
transformation instead ofReplace
. The difference is thatSetAttributes
does not touch child nodes.These techniques are much better than remove+insert because existing nodes are not moved to the bottom of their parent node. New nodes are appended at the end. Existing nodes stay where they are in the source file.
This answer applies only to newer versions of Visual Studio (2012 or newer).
对我来说更好的方法是仅在元素不存在时插入该元素,因为我只设置某些属性。删除该元素将丢弃主元素的任何其他属性(如果存在)。
例子:
web.config(不带元素)
web.config(带元素)
使用带有 XPath 表达式的定位器,如果节点不存在,则添加该节点,然后设置我的属性:
生成的两个 web.config 文件都具有 includeExceptionDetailInFaults="true"第二个保留 httpsHelpPageEnabled 属性,而删除/插入方法则不会。
A better method for me was to insert the element only if it doesn't exist since I am only setting certain attributes. Removing the element would discard any other attributes of the main element if they existed.
example:
web.config (without element)
web.config (with element)
Using the Locator with an XPath expression, I add the node if it doesn't exist and then set my attribute:
both resulting web.config files have includeExceptionDetailInFaults="true" and the second one preserves the httpsHelpPageEnabled attribute where the remove/insert method would not.