web.config Transformations in Visual Studio 2010
Every ASP.NET application contains one or more web.config files. These files store configuration details of the web application. Before Visual Studio 2010, we had to manually change the web.config file settings (set at the time of development) prior to deployment.
Visual Studio 2010 now comes with a new and interesting feature named "web.config transformation". It enables you to automatically modify or transform the application's web.config file during deployment. Visual Studio 2010 allows you to have multiple application web.config files. If you create a new web application, you'll notice that there are two versions of web.config file by default — web.Debug.config and web.Release.config. If your application is set to debug mode, the web.Debug.config file would be used at the time of deployment. If your application is set to release mode, the web.Release.config file is used as the application's configuration file. Adding web.config transformations is simple — just select the application's web.config file, right-click on it, and then select Add Config Transforms.
Here is how the new transform for release configuration would look:
<?xml version="1.0"?> <configuration xmlns:xdt=http://schemas.microsoft.com/XML-Document-Transform> <system.web> <compilation xdt:Transform="RemoveAttributes(debug)" /> </system.web> </configuration>
Now, suppose we have specified a connection string setting in the web.config file, as shown below:
<connectionStrings> <add name="Test" connectionString="Some connection string" providerName="System.Data.SqlClient" /> </connectionStrings>
As there is no Transform attribute associated with the latter snippet, this connection string setting would apply to all versions of web.config file at the time of deployment.