我可以使用Web Deploy将元素插入到web.config中吗?

问题描述 投票:9回答:4

是否可以使用Web Deploy的Parameters.xml system将XML元素插入到我的web.config中?

XmlFile参数“kind”似乎最接近我的需要,但它的match属性只接受XPath查询,我似乎无法在我的XPath查询中指定不存在的元素。 (或者说,我可以指定一个不存在的元素 - Web Deploy只是忽略它。)具体来说,我想改变它:

<configuration>
   <plugins>
      <add name="bleh"/>
   </plugins>
</configuration>

进入这个:

<configuration>
   <plugins>
      <add name="bleh">
        <option name="foo" value="bar"/>
      </add>
   </plugins>
</configuration>

(不幸的是,我无法使用空的option元素预先存储web.config,因为这个特定的插件系统不喜欢无法识别/空的选项。)

谢谢你的任何想法!

.net xslt xpath msdeploy webdeploy
4个回答
3
投票

现在可以从Web Deploy V3开始实现这些功能。见official documentation

以下是一个parameters.xml文件的示例,该文件将newNode添加到所有节点,包括目标xml文件中的根:

<parameters>
  <parameter name="Additive" description="Add a node" defaultValue="&lt;newNode />" tags="">
    <parameterEntry kind="XmlFile" scope=".*" match="//*" />
  </parameter>
</parameters>

1
投票

Xpath只是XML文档的查询语言 - 它本身不能更改XML文档或创建新的XML文档。

专门用于转换XML文档的语言称为XSLT。

这是一个非常简短的XSLT转换,可以解决您的问题:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="add[@name='bleh']">
  <xsl:copy>
   <xsl:copy-of select="@*"/>
   <option name="foo" value="bar"/>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的XML文档时:

<configuration>
    <plugins>
        <add name="bleh"/>
    </plugins>
</configuration>

产生了想要的正确结果:

<configuration>
   <plugins>
      <add name="bleh">
         <option name="foo" value="bar"/>
      </add>
   </plugins>
</configuration>

1
投票

你考虑过使用configSource吗?这允许您将配置文件拆分为多个较小的文件。 web.config将来自:

<configuration>
   <plugins>
      <add name="bleh"/>
   </plugins>
</configuration>

使用configSource:

<configuration>
   <plugins configSource="plugins.config"/>
</configuration>

缺点是在部署期间,您将无法获得用于编辑配置值的良好UI。如果您无法使参数化工作,则需要考虑这一点。如果您正在使用安装UI,则可以为管理员编写可以创建和修改plugin.xml文件的编辑工具。


0
投票

你能使用常规的web.config转换实践吗? http://blogs.msdn.com/b/webdevtools/archive/2009/05/04/web-deployment-web-config-transformation.aspx

您可以执行下面的代码,该代码将使用下面的代码替换您的所有部分。实际上,如果是您的Web.Release.config文件,您在问题中的内容应该用您提供的部分替换整个部分。

Web.Release.config:

<plugins>
  <add name="bleh">
     <option name="foo" value="bar"/>
  </add>
</plugins>
© www.soinside.com 2019 - 2024. All rights reserved.