Sunday, February 13, 2011

Web.config transforms with .Net Framework 3.5

If you have been following the new changes that have become available with the release of .Net Framework v. 4.0, in all likelihood you have heard about Web.config transforms.

Essentially, Web.config transformations allow you to modify the Web.config file at build-time based on a specific build configuration such as Debug, Release etc.  As you can guess, this is an immensely useful feature since it now allows scenarios such as deploying different connection strings for different environments incredibly easy.  You simply add the required connectionStrings to the respective build configuration file (such as Staging, Dev, Test, Production etc.) and then build the application with the necessary build configuration.

If you are not yet familiar with Web.config transformation syntax, you can read more about it here:
Web.config Transformation Syntax for Web Application Project Deployment


One of the first problems that you will encounter through most of the documentation posted on the Web is that Microsoft's default scenario expects you to build a Web Deployment package in order to get the required Web.config output.  Well, as it turns out, that is not necessary.  The article below describes how to build the required functionality simply using MSBuild:

http://andrewtwest.com/2010/02/25/using-web-config-transformations-in-web-site-projects/

Of course, the second problem you will encounter throughout the documentation is that the assumption is you are building a .Net Framework v. 4.0 project.  Well, what if you are still using .Net Framework v. 3.5?  Fear not, you can use Web.config transformations as well.

What the documentation will not tell you, is that even though this functionality was built with the release of .Net Framework v. 4.0,  you do not need to use a .Net v. 4.0 project to utilize these features.  As you can probably guess, you simply need to build your .Net Framework v. 3.5 project either with Visual Studio 2010 or MSBuild 4.0.

If you have previously upgraded a Web Application project from Visual Studio 2008, Visual Studio 2010 will not automatically add the default Web.Debug.config and Web.Release.config files to your Web Application project.  Therefore, in order to get samples of these files, you can do the following:


  1. Launch Visual Studio 2010
  2. Create a new ASP.Net 3.5 Web Application project
  3. Notice that the Web.Debug.config and Web.Release.config files are created as part of the project
  4. Save the new project and close the solution
  5. Open your existing Web Application project (upgraded from Visual Studio 2008)
  6. Right click on the project file and select Add-->Existing Item
  7. Browse to the location of the previously created ASP.Net 3.5 Web Application project
  8. Add both the Web.Debug.config and Web.Release.config files to your project
  9. Save your project with the newly added files
You should now be able to utilize Web.config transformations as part of your ASP.Net 3.5 Web Application project!

No comments:

Post a Comment