Is there a way to "insert" a new section into Web.Config using transforms? Is there a way to "insert" a new section into Web.Config using transforms? asp.net asp.net

Is there a way to "insert" a new section into Web.Config using transforms?


Found the solution that worked. Within my Web.Azure.Config file I had to add the following:

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

I tried this before posting the question but due to a typo in another part of Web.Config it was erroring.


It seems that the best solution for deployment would be to specify in the Web.Azure.Config, as you specified in your answer.

Just for fun, posting this XSLT solution that you could also use to either add the <security> element with the IP address if it did not exist, or invoke later to add additional entries. Set the IP address in the ipAddress parameter when executing. If no ipAddress is specified, it does nothing.

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