SlowCheetah not transforming web.config on build

孤街醉人 提交于 2019-11-29 04:16:58

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)' == ''">

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>

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" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!