Web.config File Transformations

Introduction

Web.config transformations allows us to modify the web.config which is used when an application is deployed.

This is important because it allows us to have checked/prebuilt configurations for each application. When you deploy a Web site, you often want some settings in the deployed application's Web.config file to be different from the development Web.config file. For example, you might want to disable debug options or change connection strings; so that they point to a different database.

Implementation

To add the new configurations, go to ConfigurationManager.

ConfigurationManager

Add a new configuration

new configuration

Add a new configuration named "Test" and select Copy Settings from <Empty>. If you want to copy setting from existing debug/release file, you can select debug/release option.

Add a new configuration

On the Web.config, right-click and select "Add Config Transform".

Add Config Transform

This adds web.config transformations to the project file.

code

If, inside web.config, you have a value named "ApplicationConnectionString", you can transform the value of this key depending on the environment you need.

code

Inside your Web.Test.config, you can specify transformations and values that are specific to the Test environment. For example,  I am transforming the "ApplicationConnectionString" to be the value "Test Environment db Connection string".

Do Not Forget To Add The Tranformation And Locator Attributes


The Match locator attribute identifies the add element in the connectionStrings section, as the element to be changed. The SetAttributestransform attribute specifies that this element's connectionString attribute should be changed to "Test Db Connection String ".

code

If you want to make sure that these changes were properly written, you can preview the changes by right clicking on the transformation file and selecting "Preview Transform".

code

Do Not Forget To Add The Tranform And Locator Attributes

Up Next
    Ebook Download
    View all
    testing
    Read by 48 people
    Download Now!
    Learn
    View all