在Web部署之外应用Web.Config转换

Jon*_*onn 10 asp.net web-config

有没有办法在Web部署之外应用VS 2010 Web.Config转换,比如说在调试期间?能够在不同环境之间自由切换,这将给我一个很大的推动力.

Enr*_*lio 16

是的,您可以通过在项目文件TransformXml中的AfterBuild步骤期间调用MSBuild任务来显式执行Web.config转换.

这是一个例子:

<UsingTask
    TaskName="TransformXml"
    AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll" />

<Target Name="AfterBuild" Condition="exists('Web.$(Configuration).config')">
    <!-- Generates the transformed Web.config in the intermediate directory -->
    <TransformXml
        Source="Web.config"
        Destination="$(IntermediateOutputPath)Web.config"
        Transform="Web.$(Configuration).config" />
    <!-- Overwrites the original Web.config with the transformed configuration file -->
    <Copy
        SourceFiles="$(IntermediateOutputPath)Web.config"
        DestinationFolder="$(ProjectDir)" />        
</Target>
Run Code Online (Sandbox Code Playgroud)

相关资源:

  • 是的,但为什么不在构建目录而不是源目录中应用转换呢?源不需要转换,只需要转换Web.config的构建版本。应用程序最终会查看构建应用程序的旁边的 Web.config,而不是我们正在处理源文件的解决方案文件夹的根目录。 (2认同)