有什么方法可以执行“替换或插入”操作吗?使用 web.config 转换?

发布于 2024-11-02 16:01:10 字数 613 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

顾忌 2024-11-09 16:01:10

在 VS2012 中与 xdt:Transform="Remove" 结合使用 xdt:Transform="InsertIfMissing"

<authorization xdt:Transform="Remove" />
<authorization xdt:Transform="InsertIfMissing">
  <deny users="?"/>
  <allow users="*"/>
</authorization>

In conjunction with xdt:Transform="Remove" use xdt:Transform="InsertIfMissing" in VS2012.

<authorization xdt:Transform="Remove" />
<authorization xdt:Transform="InsertIfMissing">
  <deny users="?"/>
  <allow users="*"/>
</authorization>
我最亲爱的 2024-11-09 16:01:10

我找到了一个廉价的解决方法。如果您有很多需要“替换或插入”的元素,它并不漂亮,也不会很好地工作。

执行“删除”,然后执行“InsertAfter|InsertBefore”。

例如,

<authorization xdt:Transform="Remove" />
<authorization xdt:Transform="InsertAfter(/configuration/system.web/authentication)">
  <deny users="?"/>
  <allow users="*"/>
</authorization>

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,

<authorization xdt:Transform="Remove" />
<authorization xdt:Transform="InsertAfter(/configuration/system.web/authentication)">
  <deny users="?"/>
  <allow users="*"/>
</authorization>
暖心男生 2024-11-09 16:01:10

使用 InsertIfMissing 转换确保 appSetting 存在。
然后使用 Replace 转换来设置其值。

<appSettings>
  <add key="Environment" xdt:Transform="InsertIfMissing" xdt:Locator="Match(key)" />
  <add key="Environment" value="Live" xdt:Transform="Replace" xdt:Locator="Match(key)" />
</appSettings>

您还可以使用 SetAttributes 转换而不是 Replace。不同之处在于 SetAttributes 不触及子节点。

<appSettings>  
  <add key="UseLivePaymentService" xdt:Transform="InsertIfMissing" xdt:Locator="Match(key)" />
  <add key="UseLivePaymentService" value="true" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>

这些技术比删除+插入要好得多,因为现有节点不会移动到其父节点的底部。新节点附加在末尾。现有节点保留在源文件中的位置。

此答案仅适用于较新版本的 Visual Studio(2012 或更高版本)。

Use the InsertIfMissing transformation to ensure that the appSetting exists.
Then use the Replace transformation to set its value.

<appSettings>
  <add key="Environment" xdt:Transform="InsertIfMissing" xdt:Locator="Match(key)" />
  <add key="Environment" value="Live" xdt:Transform="Replace" xdt:Locator="Match(key)" />
</appSettings>

You could also use the SetAttributes transformation instead of Replace. The difference is that SetAttributes does not touch child nodes.

<appSettings>  
  <add key="UseLivePaymentService" xdt:Transform="InsertIfMissing" xdt:Locator="Match(key)" />
  <add key="UseLivePaymentService" value="true" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>

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).

無處可尋 2024-11-09 16:01:10

对我来说更好的方法是仅在元素不存在时插入该元素,因为我只设置某些属性。删除该元素将丢弃主元素的任何其他属性(如果存在)。

例子:
web.config(不带元素)

<serviceBehaviors>
    <behavior name="Wcf.ServiceImplementation.AllDigitalService_Behavior">
        <serviceMetadata httpGetEnabled="true" />
    </behavior>
</serviceBehaviors>

web.config(带元素)

<serviceBehaviors>
    <behavior name="Wcf.ServiceImplementation.AllDigitalService_Behavior">
        <serviceDebug httpsHelpPageEnabled="true" />
        <serviceMetadata httpGetEnabled="true" />
    </behavior>
</serviceBehaviors>

使用带有 XPath 表达式的定位器,如果节点不存在,则添加该节点,然后设置我的属性:

<serviceDebug xdt:Transform="Insert"
  xdt:Locator="XPath(/configuration/system.serviceModel/behaviors/serviceBehaviors/behavior[not(serviceDebug)])" />
<serviceDebug includeExceptionDetailInFaults="true" xdt:Transform="SetAttributes" />

生成的两个 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)

<serviceBehaviors>
    <behavior name="Wcf.ServiceImplementation.AllDigitalService_Behavior">
        <serviceMetadata httpGetEnabled="true" />
    </behavior>
</serviceBehaviors>

web.config (with element)

<serviceBehaviors>
    <behavior name="Wcf.ServiceImplementation.AllDigitalService_Behavior">
        <serviceDebug httpsHelpPageEnabled="true" />
        <serviceMetadata httpGetEnabled="true" />
    </behavior>
</serviceBehaviors>

Using the Locator with an XPath expression, I add the node if it doesn't exist and then set my attribute:

<serviceDebug xdt:Transform="Insert"
  xdt:Locator="XPath(/configuration/system.serviceModel/behaviors/serviceBehaviors/behavior[not(serviceDebug)])" />
<serviceDebug includeExceptionDetailInFaults="true" xdt:Transform="SetAttributes" />

both resulting web.config files have includeExceptionDetailInFaults="true" and the second one preserves the httpsHelpPageEnabled attribute where the remove/insert method would not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文