In all project it’s a real nightmare to manage configuration files. In Silverlight projects like in web application, you can take advantage of Web Application Project Deployment features of Visual Studio 2010. In this blog, I’ll explain you :

  • How to create a new configuration (part 1);
  • How to create package definition (part 1);
  • How to create package (part 1);
  • How to publish the application (part 1);
  • How to use transformation with web.config files (part 2);
  • Some more links on this subject (part 2).

In this post, I won’t explain SQL deployment.

How to use transformation with web.config files

In this post, I’ll explain how to use Web.config Transformation Syntax for Web Application Project Deployment. I’ll demonstrate this mechanism with two samples:

  • transformation for debug configuration;
  • transformation for staging configuration;


In your web.config file, you will define all parameters that you need in your application.  For example, you need a DB connection string named MYDB. You need to define the connectionString add element with a specific name but without any connectionString value. You can also define, for example, compilation target to framework 4.0, debug mode. Finally, you disable custom errors.

Code Snippet
  1. <?xml version="1.0"?>
  2. <configuration>
  3.   <connectionStrings>
  4.       <add name="MyDB"connectionString="..." />
  5.     </connectionStrings>
  6.     <system.web>
  7.       <compilation debug="true" targetFramework="4.0" />
  8.       <customErrors mode="Off"></customErrors>
  9.     </system.web>
  10.  
  11.     <system.webServer>
  12.       <modules runAllManagedModulesForAllRequests="true"/>
  13.     </system.webServer>
  14.  
  15. </configuration>

This Web.Config file will be used/transformed to be compliant with the desired configuration.

Web.Debug.config

In the Web.Debug.Config file, you’ll create some transformation with a specific syntax. In this example, I’ll just add a specific connectionString dedicated to debug environment. In the Add element, you just need to add xdt:Transform and wdt:Locator=”Match(name)”. Those two attributes will inform that the transformation will be applied on an “add” xml element where the attribute name=”MyDB”, connectionString attribute will contains the specific connection string.

Code Snippet
  1. <?xml version="1.0"?>
  2. <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  3.   <connectionStrings>
  4.     <add name="MyDB"
  5.       connectionString="Data Source=MySQLServer;Initial Catalog=MyDevDB;Integrated Security=True"
  6.       xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
  7.   </connectionStrings>
  8.   <system.web>
  9.  
  10.   </system.web>
  11. </configuration>

Here is the result of the translation after compiling and publishing the application.

Code Snippet
  1. <?xml version="1.0"?>
  2. <configuration>
  3.   <connectionStrings>
  4.       <add name="MyDB"connectionString="Data Source=MySQLServer;Initial Catalog=MyDevDB;Integrated Security=True" />
  5.     </connectionStrings>
  6.     <system.web>
  7.       <compilation debug="true" targetFramework="4.0" />
  8.       <customErrors mode="Off"></customErrors>
  9.     </system.web>
  10.  
  11.     <system.webServer>
  12.       <modules runAllManagedModulesForAllRequests="true"/>
  13.     </system.webServer>
  14.  
  15. </configuration>
Web.Staging.config

You could also make some replacement or element removal and of course another DB connectionString replacement:

Code Snippet
  1. <?xml version="1.0"?>
  2. <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  3.   <connectionStrings>
  4.     <add name="MyDB"
  5.       connectionString="Data Source=MySQLServer;Initial Catalog=MyStagingDB;Integrated Security=True"
  6.       xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
  7.   </connectionStrings>
  8.   <system.web>
  9.     <customErrors defaultRedirect="GenericError.htm" mode="RemoteOnly" xdt:Transform="Replace">
  10.       <error statusCode="500" redirect="InternalError.htm"/>
  11.     </customErrors>
  12.     <compilation xdt:Transform="RemoveAttributes(debug)" />
  13.   </system.web>
  14. </configuration>
Here is the result of the translation.
Code Snippet
  1. <?xml version="1.0"?>
  2. <configuration>
  3.   <connectionStrings>
  4.       <add name="MyDB"connectionString="Data Source=MySQLServer;Initial Catalog=MyStagingDB;Integrated Security=True" />
  5.     </connectionStrings>
  6.     <system.web>
  7.       <compilation targetFramework="4.0" />
  8.       <customErrors defaultRedirect="GenericError.htm" mode="RemoteOnly">
  9.           <error statusCode="500" redirect="InternalError.htm"/>
  10.       </customErrors>
  11.     </system.web>
  12.  
  13.     <system.webServer>
  14.       <modules runAllManagedModulesForAllRequests="true"/>
  15.     </system.webServer>
  16.  
  17. </configuration>

This mechanism is very helpfull, hoping that this helps.

Some more links on this subject

ASP.NET Web Application Project Deployment Overview
http://msdn.microsoft.com/en-us/library/dd394698(VS.100).aspx

Web.config Transformation Syntax for Web Application Project Deployment
http://msdn.microsoft.com/en-us/library/dd465326(VS.100).aspx

Web Deployment: Web.Config Transformation
http://blogs.msdn.com/webdevtools/archive/2009/05/04/web-deployment-web-config-transformation.aspx