Search code examples
web.config-transformconfig-transformationweb-config

Remove section in web.config file in debug mode


I have an asp.net application, which must run under SSL, and it has some rewrite rules defined in web.config to accomplish this.

<!--file web.config -->
....
</system.webServer>
  <rewrite>
    <rules configSource="webrewrite.config" />
  </rewrite>
</system.webServer>


<!--file web.config -->
<rules>
  ....    
  <rule name="HTTP to HTTPS redirect" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
      <add input="{HTTPS}" pattern="off" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
  </rule>
  ....
</rules>

However, in development mode (with local web server or IIS Express) I don't want to use SSL. So I would like to be able to use web.config transformations to remove one or more rewrite rules (but not all)


Solution

  • I solved the problem, by using Remove transform, as shown below

    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
      ....
      <system.webServer>
        <rewrite>
          <rules>
            <rule name="RulaNameToRemove"
              xdt:Transform="Remove"
              xdt:Locator="Match(name)" >
            </rule>
          </rules>
        </rewrite>    
      </system.webServer>
    </configuration>