Applying Web.Config transformations outside of Web Deployment

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 between different environments.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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)MicrosoftVisualStudiov10.0WebMicrosoft.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:

Method 2

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)MicrosoftVisualStudiov10.0WebMicrosoft.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>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x