【问题标题】:Is there a way to "insert" a new section into Web.Config using transforms?有没有办法使用转换将新部分“插入”到 Web.Config 中?
【发布时间】:2014-07-05 18:24:08
【问题描述】:

在我的 Web.Config 中,我有以下内容

  <system.webServer>

    <modules>
       **some code**
    </modules>
    <handlers>
       **some code**    
    </handlers>

  </system.webServer>

如何对其进行转换,以便将“安全”的新子部分注入“system.webServer”?到目前为止,我尝试和搜索的一切都失败了。

我想要的如下所示:

  <system.webServer>

    <modules>
       **some code**
    </modules>
    <handlers>
       **some code**    
    </handlers>

    <security>
      <ipSecurity allowUnlisted="false" denyAction="NotFound">
        <add allowed="true" ipAddress="10.148.176.10" />
      </ipSecurity>
    </security>

  </system.webServer>

【问题讨论】:

  • 据我所知,web.config 在应用程序启动时只读取一次,即使你可以,但这并不意味着它会在应用程序重新启动之前生效。
  • 更改web.config 将触发应用重启。您当然可以在部署期间通过转换生成它,但看起来 OP 想要使用基于服务器本身的 IP 地址。这可能是可能的(取决于托管设置),尽管它与 XSLT 本身无关。
  • @harpo 澄清一下,我希望能够在部署期间使用转换添加 部分。干杯保罗

标签: asp.net xslt web-config-transform xdt-transform


【解决方案1】:

找到有效的解决方案。在我的 Web.Azure.Config 文件中,我必须添加以下内容:

  <system.webServer>
    <security xdt:Transform="Insert">
      <ipSecurity allowUnlisted="false" denyAction="NotFound">
        <add allowed="true" ipAddress="10.148.176.10" />
      </ipSecurity>
    </security>
  </system.webServer>

我在发布问题之前尝试过此操作,但由于 Web.Config 另一部分的拼写错误,因此出错了。

【讨论】:

  • 为了清楚起见,重要的部分是xdt:Transform="Insert"。具有此属性的任何元素都将插入到输出中。别忘了你可以在VS中使用“Preview Transform”来快速查看一个transform是否达到了你期望的效果。
【解决方案2】:

似乎最好的部署解决方案是在 Web.Azure.Config 中指定,正如您在答案中指定的那样。

只是为了好玩,发布这个 XSLT 解决方案,您也可以使用它来添加带有 IP 地址的 &lt;security&gt; 元素(如果它不存在),或者稍后调用以添加其他条目。执行时在ipAddress参数中设置IP地址。如果没有指定ipAddress,它什么也不做。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:param name="ipAddress"/>

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

    <!--Create security/ipSecurity with specified IP address, 
        if specified in param-->
    <xsl:template match="system.webServer[not(security)]">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
            <xsl:if test="$ipAddress">
                <security>
                    <ipSecurity allowUnlisted="false" denyAction="NotFound">
                        <add allowed="true" ipAddress="{$ipAddress}" />
                    </ipSecurity>
                </security>
            </xsl:if>
        </xsl:copy>      
    </xsl:template>

    <!--Add an allowed IP address to existing security/ipSecurity entry, 
        if IP address is specified in param -->
    <xsl:template match="security/ipSecurity">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
            <xsl:if test="$ipAddress">
                <add allowed="true" ipAddress="{$ipAddress}" />
            </xsl:if>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 2011-08-09
    • 2010-12-04
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多