SlowCheetah not transforming web.config on build

后端 未结 3 1151
北荒
北荒 2020-12-14 23:54

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 transform

相关标签:
3条回答
  • 2020-12-15 00:45

    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" />
    
    0 讨论(0)
  • 2020-12-15 00:51

    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>
    
    0 讨论(0)
  • 2020-12-15 00:56

    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)' == ''">
    
    0 讨论(0)
提交回复
热议问题