Easy Testing Of Your Web.Config Transformations

Published on
Reading time
Authors

One of the powerful features of ASP.NET 4.0 was the introduction of web.config transformations that meant you could now do with ASP.NET out-of-the-box what you would have previously done with some form of custom XSLT transform in your build process. One thing that is not that easy is to test the outputs from the transformations.

One option is the simple online web.config tester from the guys over at AppHarbor.  While that's great, personally I don't want to round-trip my web.config files over the internet just to test something I should be able to do locally. The result was that after some playing I found a way to test locally utilising msbuild with the right parameters.

The one proviso to this simple test working is that you have successfully compiled the code for you web application (either via msbuild command-line or inside Visual Studio).  This test will fail if the binaries or other inputs for your package aren't available.

All you need to do is issue this command line:

MSBuild.exe YourProject.csproj /T:Package /P:DeployOnBuild=True;Configuration=YourConfiguration

You will now find in the 'obj' folder of the project you targetted a set of folders - if you dig through them you will find a "TransformsWebConfig" sub-folder that will contain the output result of your transform.

Happy Days!

Updated!

New in Visual Studio 2012 is the ability to "Preview Transform" on configuration files that utilise the above technique.  Open your solution in Visual Studio, expand the transformation node of your config file, select the transform to review and choose "Preview Transform" from the menu.  Grab a look at screenshots either at Hanselman's blog or here.