18 March 2011

Simple Web.config Transformation without Visual Studio 2010

 

The MSDN documentation on Web.Config Transformation does not really help regarding generating the transformed config files in Visual Studio 2010, without going though full publications and involving the other solution projects.

For generating the most low-impact Transform operation outsite Visual Studio you have to add the following to your C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets and call msbuild with the task as \target, this could be put as a post build event, this way you can always check that your configuration transformations are merged as expected instead of having to guess, otherwise a fantastic new feature in the framework.

call "msbuild" "D:\Solutions\Intranet.Web.csproj" /t:Transform /p:Configuration=Release


You can also add it to the *.csproj file but it would be removed by VS upon editing, the other file is included by VS into the csproj.

   1: <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
   2: <Target Name="Transform">
   3:   <MakeDir Directories="obj\$(Configuration)" Condition="!Exists('obj\$(Configuration)')" />
   4:   <TransformXml Source="Web.Config" Transform="Web.$(Configuration).config" Destination="obj\$(Configuration)\Web.config" StackTrace="true" />
   5: </Target>

Source: http://forums.asp.net/p/1532038/3789356.aspx

2 comments:

Lars said...

Nice post!
As it's possible to create your own configurations within VS, you can easily create different transformations for various servers in a production environment, giving for instance a configuration for the content management node and a different configuration for the delivery nodes.

/Lars

Frederik said...

I agree it's a fantastic feature, they should have added to VS years ago.
And with VS10 SP1 they also added the feature to WCF projects. Yay! :-)