首页 文章

SlowCheetah没有在构建时转换web.config

提问于
浏览
7

我通过nuget安装了SlowCheetah软件包,并为我的web.config添加了基于Build config的转换文件 . 但是,在构建时,web.config不会被转换 . 我检查了我的项目文件,确实看到了SlowCheetah PropertyGroup和Import元素的条目 . 我没有在项目文件中看到转换目标 . 如果我添加app.config,app.config文件确实会被转换 . 我的理解是,安装SlowCheetah软件包应该自动将web.config转换目标添加到项目的MSBuild文件中 . 我可以手动添加它,但我认为SlowCheetah开箱即用 . 我错过了什么 . 请告诉我 . 我的要求是我的web.config文件应该根据构建配置进行转换,转换后的web.config文件应该位于输出目录中 . 谢谢,感谢所有的帮助 .

3 回答

  • 5

    只有在使用发布功能部署项目时,Visual Studio才会进行转换 . 要在进行构建时执行此操作,您需要调整MSBuild脚本 . 完整的解决方案是here . 这里的要领:

    项目中的文件除现有的Web.Debug.config和Web.Release.config外,创建名为Web.Base.config的文件 . 此文件将等同于旧的Web.config,因为它将成为转换的基础 . 您将最终得到这些文件:Web.config,Web.Base.config,Web.Debug.config,Web.Release.config和Web.config . 将以下配置添加到.csproj文件的底部,就在结束-tag之前:

    <Target Name="BeforeBuild">
        <TransformXml Source="Web.Base.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
    </Target>
    

    更新:从评论中的提醒我意识到,在发布项目时,我也遇到了Visual Studio两次转换XML的问题 . 解决方案是向Target标记添加一个Condition,如下所示:

    <Target Name="BeforeBuild" Condition="'$(PublishProfileName)' == '' And '$(WebPublishProfileFile)' == ''">
    
  • 0

    为了详细说明Philipp的答案,我发现了一个可能更简单的解决方案:不需要Web.Base.Config,并且没有覆盖web.config的问题,这会导致Source Control出现问题 .

    BeforeBuild:您将TargetDir和TargetFileName作为目标 . AfterBuild:您在构建完成后将其复制到已发布的网站 .

    <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>
    
  • 15

    您是否将转换文件的“复制到输出目录”属性设置为“不复制”?请检查您的项目文件 .

    在项目文件中,应添加以下条目(取决于您安装的版本,在本例中为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" />
    

相关问题