Msbuild SlowCheetah未在生成时转换web.config

Msbuild SlowCheetah未在生成时转换web.config,msbuild,slowcheetah,Msbuild,Slowcheetah,我通过nuget安装了SlowCheetah包,并基于Build config为我的web.config添加了转换文件。 但是,在构建时,web.config不会被转换。我检查了我的项目文件,确实看到了SlowCheetah PropertyGroup和Import元素的条目。我在项目文件中没有看到要转换的目标。 如果我添加一个app.config,app.config文件会被转换。 我的理解是,安装SlowCheetah包应该自动将web.config转换目标添加到项目的MSBuild文件中。

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

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

应在项目文件中添加以下条目(取决于您安装的版本,在本例中为2.5.7):


真的
$([System.IO.Path]::GetFullPath($(MSBuildProjectDirectory)\..\packages\SlowCheetah.2.5.7\tools\SlowCheetah.Transforms.targets))
$(缓慢的输入路径)


仅当您使用发布功能部署项目时,Visual Studio才会执行转换。要在生成时执行此操作,需要调整MSBuild脚本。完整的解决方案是。以下是要点:

project中的文件创建一个名为Web.Base.config的文件,除了 现有Web.Debug.config和Web.Release.config。此文件将被删除 等效于旧的Web.config,因为它将是 转化。您将得到以下文件:

Web.config、Web.Base.config、Web.Debug.config、Web.Release.config和 Web.config。将以下配置添加到页面底部 .csproj文件,就在关闭标记之前:


更新:从评论中的提醒我意识到我也有一个 发布时Visual Studio两次转换XML时出现问题 一个项目。解决方法是向 目标标签如下:


为了详细说明Philipp的答案,我找到了一个可能更简单的解决方案: 不需要Web.Base.Config,覆盖Web.Config也不会导致源代码管理出现问题

BeforeBuild:将目标设置为TargetDir en TargetFileName

后构建:在构建完成后,将其复制到已发布的网站



关于解决方案,对于Visual Studio 2013,您不需要,因为任务已经导入。感谢分享。yas yas yas!问题是关于SlowCheetah的,它是一个扩展+NuGet包,应该为您做这件事。需要考虑的一个缺点是:作为Jacob Viau,这将破坏修改
Web.config
的NuGet包安装。您能否解释一下为什么构建不会替换在构建之前复制到那里的配置文件?因为我无法让它工作,我想这是因为文件被覆盖了。对于任何观看的人来说,这对于当前版本的SlowCheetah来说是100%不正确的。
<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" />
<Target Name="BeforeBuild">
    <TransformXml Source="Web.Base.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
</Target>
<Target Name="BeforeBuild" Condition="'$(PublishProfileName)' == '' And '$(WebPublishProfileFile)' == ''">
<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>