Friday, May 14, 2010

VS 2010 – Cool New Feature – Hierarchical Web.Config files

One of the coolest new features in VS 2010 is what I call “hierarchical web.config files”. The idea behind this feature is that you have one main web.config file that contains all the settings needed to run the application in your local environment. You then have multiple web.config files that inherit from this main web.config file, in which you specify only the changes needed on the main web.config file for your different environment.

I believe the official name for this feature is “web.config transformations”.

Note 1: An important point to note is that hierarchical web.config files is only available on web-application projects and not for web-site projects. This feature is also not available for app.config files.

Here is an example:

The main file, defines all the settings required by the web application.

The debug file, changes the connection string to use the TestStage server instead of the local “.\SqlExpress” defined in the main config file. This is done by using the xdt:Transform and the xdt:Locator attributes (shown in bold).

The production file, not only changes the connection string to use the production database server, it also removes the debug=”true” attribute, that was specified in the main.config file. (this on its own, makes this feature worth it to me – coz, I cant even begin counting the number of times I have pushed code to production and forgotten to remove this setting).

image

Note 2: The settings in Debug and Production do not take effect when you are debugging the application within Visual Studio (F5). The transformations specified only take effect when you publish or deploy the application and at that time, the transformation applied is dependent on the selected configuration.

More information:

http://msdn.microsoft.com/en-us/library/dd465326(VS.100).aspx

No comments: