How to add extra configuration to web.config for Release environment

As you are aware of that you might need to added extra configuration to web.config when you release your ASP.NET project.

You can easily do this using File Transformations concept.

The key point here is to add xdt:Transform="Insert" in the root tag hierarchy you want to be added for the respective environment.

By default you have your Web.config file, but you have also Web.Debug.config and Web.Release.config as seen in the image below:

Web.config File Transformations
Web.config File Transformations

Lets say you want to added a redirection from http to https in your release of the application. Then edit Web.Release.config and add following lines:

<?xml version="1.0"?>
.....
<system.webServer>
    <rewrite xdt:Transform="Insert">
        <rules>
           ......
        </rules>
    </rewrite>
</system.webServer>
</configuration>

So next time you publish your project the tag with rewrite and its sub-content will be added to web.config file.

To see that before you publish, right click on Web.Release.config and click Preview Transform.

Preview Transform web.config
Preview Transform of Web.Config

You will see the difference between initial version and release version.

File Transformations changes
Preview File Transformations changes

Get more details about File Transformation from Microsoft.

Enjoy.

Leave a Comment