Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 334
Jonn Avatar asked Jun 22 '11 08:06

Jonn


People also ask

How does Web config transform work?

A Web. config transformation file contains XML markup that specifies how to change the Web. config file when it is deployed. You can specify different changes for specific build configurations and for specific publish profiles.

How do I create a new transformation in Web config?

If you have a web application project, Right-click on web. config and choose Add Config Transform. This will add any config transforms that are missing from your project based on build configurations (i.e. if you have Production and Staging build configs, both will get a transform added).

Can a web application run without Web config?

Yes, we can run an Asp.Net web application without web. config file but without in debugging mode. If we don't configure any settings in web. config file then it consider machine.

What is Xdt transform in Web config?

A transform file is an XML file that specifies how the Web. config file should be changed when it is deployed. Transformation actions are specified by using XML attributes that are defined in the XML-Document-Transform namespace, which is mapped to the xdt prefix.


2 Answers

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?
like image 81
Enrico Campidoglio Avatar answered Oct 13 '22 20:10

Enrico Campidoglio


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>
like image 24
Bernd Avatar answered Oct 13 '22 18:10

Bernd