I installed the SlowCheetah package via nuget and added transform files for my web.config based on Build config. However, on building, the web.config does not get transformed. I checked my project file and did see entries for SlowCheetah PropertyGroup and Import elements. I dont see a target for transformation in the project file. If I add an app.config, the app.config file does get transformed. It is my understanding that installing the SlowCheetah package should automatically add the web.config transform target to the MSBuild file for the project. I can add it manually but I thought SlowCheetah does it out of the box. Am I missing something. Please do let me know. My requirement is that my web.config file should get transformed based on build configuration and the transformed web.config file should be located in the output directory. Thanks and appreciate all help.
3 Answers
Visual Studio is doing Transformation only when you deploy your project by using the publish functionality. To do it when you do the build you need to tweak your MSBuild script. The complete solution is here. Here the essentials:
Files in project Create a file named Web.Base.config, besides the existing Web.Debug.config and 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:
Web.config, Web.Base.config, Web.Debug.config, Web.Release.config, and Web.config. Add the following configuration to the bottom of your .csproj-file, just before the closing -tag:
<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)' == ''">

- 721
- 7
- 22
-
2Regarding the solution, for Visual Studio 2013, you don't need the
since the – MikeJansen Sep 10 '14 at 17:19task is already imported. -
1Thanks for sharing. – Philipp Stauss Jun 06 '16 at 20:44
-
1yas yas yas yas ! – TWilly Sep 29 '16 at 19:27
-
6The question was about SlowCheetah, which is an extension+NuGet package that should do this for you. – Rudey Jul 07 '17 at 13:23
-
1One drawback to consider: As Jacob Viau [points out](https://github.com/Microsoft/slow-cheetah/issues/64#issuecomment-314595565), this will break NuGet package installations that modify `Web.config`. – Eric Eskildsen Jan 19 '18 at 17:38
-
I'm getting the page cannot be run in debug mode because debuggging is not enable in the Web.config file – Demodave Apr 13 '18 at 20:58
-
The question is about SlowCheetah. – Han Moe Htet Jan 27 '23 at 08:21
To elaborate on Philipp's answer I have found a possible simpler solution: No need for a Web.Base.Config and no problems with overwriting your web.config which causes problems in Source Control.
BeforeBuild: You make the destination your TargetDir en TargetFileName.
AfterBuild: You copy this to your published websites after the build is done.
<Target Name="BeforeBuild">
<TransformXml Source="Web.config" Transform="Web.$(Configuration).config" Destination="$(TargetDir)$(TargetFileName).config" />
</Target>
<Target Name="AfterBuild" Condition="'$(Configuration)' == 'Dev' Or '$(Configuration)' == 'Test' Or '$(Configuration)' == 'Prod'">
<Delete Files="$(TargetDir)_PublishedWebsites\$(ProjectName)\Web.config" />
<Copy SourceFiles="$(TargetDir)$(TargetFileName).config" DestinationFiles="$(TargetDir)_PublishedWebsites\$(ProjectName)\Web.config" />
</Target>
-
Can you elaborate why the build wouldn't replace the config file that was copied there before the build? Because I can't get this to work, and I think it's because the file is overwritten. – Rudey Jul 07 '17 at 14:12
Did you set the "copy to output directory" property of your transform files to "Do not copy" ? Please check also your project file.
In your project file the following entries should be added (depending the version you installed , in this case 2.5.7):
<PropertyGroup Label="SlowCheetah">
<SlowCheetah_EnableImportFromNuGet Condition=" '$(SC_EnableImportFromNuGet)'=='' ">true</SlowCheetah_EnableImportFromNuGet>
<SlowCheetah_NuGetImportPath Condition=" '$(SlowCheetah_NuGetImportPath)'=='' ">$([System.IO.Path]::GetFullPath( $(MSBuildProjectDirectory)\..\packages\SlowCheetah.2.5.7\tools\SlowCheetah.Transforms.targets ))</SlowCheetah_NuGetImportPath>
<SlowCheetahTargets Condition=" '$(SlowCheetah_EnableImportFromNuGet)'=='true' and Exists('$(SlowCheetah_NuGetImportPath)') ">$(SlowCheetah_NuGetImportPath)</SlowCheetahTargets>
<Import Project="$(SlowCheetahTargets)" Condition="Exists('$(SlowCheetahTargets)')" Label="SlowCheetah" />

- 23
- 2
-
2For anyone watching, this is 100% incorrect for a current version of SlowCheetah. – EKW Oct 13 '17 at 18:47