调试代码时不会转换Web.config

mam*_*esh 21 c# app-config visual-studio slowcheetah visual-studio-2015

我有一个主Web.config文件,下面有一个Web.Test.config,Web.Development.Config等等.

当我在Test config上通过SlowCheetah预览转换时,它似乎正确地转换了值.

当我将构建环境从开发切换到测试并尝试调试应用程序时,应用程序在主Web.config文件中的任何值下运行(即它不会转换任何内容).

如何使构建环境在调试时选择正确的配置而不是总是使用基本Web.config文件?这可能吗?

Ily*_*kov 40

你可以转换Web.config构建.将此目标添加到*.csproj文件:

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\WebApplications\Microsoft.WebApplication.targets" />
<Target Name="BeforeBuild">
    <TransformXml 
        Source="Web.Base.config" 
        Transform="Web.$(Configuration).config" 
        Destination="Web.config" />
</Target>
Run Code Online (Sandbox Code Playgroud)

保持原始配置Web.Base.config.它足以启用转换,适用于任何XML配置文件.根本不再需要SlowCheetah.

http://sebnilsson.com/a5410281/asp-net-transform-web-config-with-debug-release-on-build/

  • 该解决方案工作正常。但是另一个缺点是,Visual Studio / NuGet / etc之类的任何东西。Web.config文件中的更改,需要手动移植到Web.Base.config文件中。 (2认同)
  • 如果您不想 &lt;Target Name="BeforeBuild"&gt; &lt;TransformXml Source="Web.config" Transform="Web.$(Configuration).config" Destination="Web.config" 则不必使用 base /&gt; &lt;/目标&gt; (2认同)
  • 根据提供的链接,应该更新它以纠正双重转换问题。“更新:根据评论中的提醒,我意识到在发布项目时,Visual Studio 两次转换 XML 也存在问题。解决方案是向目标标记添加一个条件,如下所示:&lt;目标名称="BeforeBuild" 条件="'$(PublishProfileName)' == '' 和 '$(WebPublishProfileFile)' == ''"&gt; " (2认同)

小智 5

XML转换仅在您发布Web应用程序时应用,而不是在构建期间应用.

博客文章详细介绍了使用构建设置的工作.

  • @arviman csproj 文件需要在两个地方进行更改` ... &lt;None Include="Web.Base.config" /&gt; &lt;Content Include="Web.config" /&gt; &lt;None Include="Web.PROD.config"&gt; &lt;DependentUpon&gt;Web.config&lt;/DependentUpon&gt; &lt;/None&gt; ... &lt;Import Project="$(SlowCheetahTargets)" Condition="Exists('$(SlowCheetahTargets)')" Label="SlowCheetah" /&gt; &lt;目标名称="BeforeBuild" Condition="'$(PublishProfileName)' == '' 且 '$(WebPublishProfileFile)' == ''"&gt; &lt;TransformXml Source="Web.Base.config" Transform="Web.$(Configuration) .config" Destination="Web.config" /&gt; &lt;/目标&gt; &lt;/项目&gt;` (2认同)