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.
This Web.Config file will be used/transformed to be compliant with the desired configuration.
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.
Here is the result of the translation after compiling and publishing the application.
You could also make some replacement or element removal and of course another DB connectionString replacement:
Here is the result of the translation.
This mechanism is very helpfull, hoping that this helps.
Some more links on this subject
ASP.NET Web Application Project Deployment Overview
Web.config Transformation Syntax for Web Application Project Deployment
Web Deployment: Web.Config Transformation