Applying Web.Config transformations outside of Web Deployment

后端 未结 2 1801
天涯浪人
天涯浪人 2021-01-02 04:05

Is there a way to apply VS 2010 Web.Config transformations outside of web deployment, say during debugging? It would give me a great boost to be able to freely switch betwee

相关标签:
2条回答
  • 2021-01-02 04:30

    The solution above made a great starting point for me, but I ended up with the following which doesn't need a copy task and doesn't have any issues with file in use errors.

    <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
    <Target Name="AfterBuild">
      <TransformXml Condition="exists('$(TempBuildDir)\Web.$(Configuration).config')" Source="$(TempBuildDir)\Web.config" Destination="$(OutputPath)Web.config" Transform="$(TempBuildDir)\Web.$(Configuration).config" />
      <ItemGroup>
        <DeleteAfterBuild Include="$(OutputPath)Web.*.config" />
      </ItemGroup>
      <Delete Files="@(DeleteAfterBuild)">
        <Output TaskParameter="DeletedFiles" PropertyName="deleted" />
      </Delete>
      <Message Text="DELETED FILES: $(deleted)" Importance="high" />
    </Target>
    
    0 讨论(0)
  • 2021-01-02 04:33

    Yes, you can perform a Web.config transformation explicitly by invoking the TransformXml MSBuild task during the AfterBuild step in your project file.

    Here's an example:

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

    Related resources:

    • Web.config transformations for App.config files
    • Can I specify that a package should be created every time I build a solution?
    0 讨论(0)
提交回复
热议问题