Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SlowCheetah not transforming web.config on build

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.

like image 337
AIyer Avatar asked Aug 30 '13 22:08

AIyer


People also ask

How do I create a new transformation in web config?

config transformation files that are created by default for the two default build configurations. You can create transformation files for custom build configurations by right-clicking the Web. config file and choosing Add Config Transforms from the context menu.

What is Slow Cheetah C#?

This package allows you to automatically transform your app. config (or any file) when you press F5 in Visual Studio. You can have different transformations based on the build configuration. This will enable you to easily have different app settings, connection strings, etc for Debug versus Release.

How web Config transform works?

XDT is a simple and straight forward method of transforming the web. config during publishing/packaging. Transformation actions are specified using XML attributes defined in the XML-Document-Transform namespace, that is mapped to the xdt prefix. There are xdt:Transform and xdt:Locator attributes that we use in the Web.

What is xdt transform replace?

A Transform attribute on a parent element can affect child elements even if no Transform is specified for them. For example, if you put the attribute xdt:Transform="Replace" in the system. web element, all the elements that are children of the system. web element are replaced with the content from the transform file.


2 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)' == ''">
like image 139
Philipp Stauss Avatar answered Oct 21 '22 21:10

Philipp Stauss


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>
like image 21
bpuntart Avatar answered Oct 21 '22 20:10

bpuntart