Techbubbles

Web.Config Transforms in Visual Studio 2010

 

This post discuss about using Web.Config Transformations in Visual Studio 2010. It is a new feature in Visual Studio 2010.

You have a very simple web.config file as shown below. In this Config file we have a very simple connection string for NorthwindDatabase. It is pointing to the local SQL Server Express instance.

image

This is ok for local development. Developers often face the challenge in managing connection strings from testing to staging and then into production. In the past developers used to manage number of different keys in web.config file to manage this scenario. With Visual Studio 2010 we have a feature called Transformations that allows us to manage those settings with simple and secure manner

Notice in Visual Studio solution explorer of your project, the web.config file allows you to open additional files as shown in the below screen. Look at the default instance of web.debug.config file which corresponds to the debug configuration. You also have the web.release.config file which corresponding to the release configuration.

image

Now create the connection strings to these different environments and these config files follow you when you publish the content to the corresponding servers. Mention the respective server names in the Release config and Debug config file as shown in the below screen.

image

We need to add set of additional strings in web.config to make this work. The parameter name called xdt:transform which is one of the number of transforms that came with VS 2010 release. You also have replace, remove all, Insert, InsertBefore etc. In the configuration file xdt:locator parameter maps the name to key database name in the config file. Copy the transformations across other(release and staging) configuration files. If you notice the release version of web.config file then it automatically removes the debug attribute from the file where as in Debug config file it is not.

image

To make sure our changes are affected , right click the web site and say publish and check the output folder. Now open the web.config file from the output folder you can see debugserver as a datasource for the connection string because we deployed the web site under debug configuration mode. Change the configuration mode to release and publish the website then you will notice the production server is automatically updated in output folder config file.

image

You can also create your own environments let us say staging and can deploy the website with relevant server names in config file.


Share this post :


Related Posts:

%d bloggers like this: