Did you ever wish you could run your web application with the
Web.config being transformed according to the current Solution Configuration for Debug or Release directly from Visual Studio? Well, with some pre-build-magic we can do this.
Files in project
Create a file named
Web.Base.config, besides the existing
Web.Release.config. This file will be the equivalent to your old
Web.config, as it will be the base for the tranformation. You will end up with these files:
Add the following configuration to the bottom of your
.csproj-file, just before the closing
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\WebApplications\Microsoft.WebApplication.targets" /> <Target Name="BeforeBuild"> <TransformXml Source="Web.Base.config" Transform="Web.$(Configuration).config" Destination="Web.config" /> </Target>
UPDATE: From a reminder in the comments I realized that I also have a problem with Visual Studio transforming the XML twice, when Publishing a project. The solution to this is to add a
Condition to the
Target-tag like this:
<Target Name="BeforeBuild" Condition="'$(PublishProfileName)' == '' And '$(WebPublishProfileFile)' == ''">
You can now build, run your project or publish it all depending on what Solution Configuration you have selected. Debug or Release. Just press your CTRL+SHIFT+B and see the
Web.config-file be transformed.
I recommend that you leave
Web.config out of your Version Control. This solution for transforming the
Web.config on the fly would mean that this specific file would constantly be modified and if one person is working with Debug and another person is working with Release the
Web.config would probably get big changes constantly.