Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

web.config file transform from command line

Tags:

msbuild

I have two build environments to target; Release and Staging. The Web.config looks like this:

<system.web>          <authentication mode="Windows">     </authentication>      <authorization>         <deny users="?" />     </authorization> </system.web> 

I want to transform it by building to staging config: Web.Staging.config

<system.web>          <authentication mode="Windows">     </authentication>      <authorization xdt:Transform="Replace">         <deny users="?" />         <allow roles="StagingRoles" />         <deny users="*" />     </authorization> </system.web> 

I build from command line like this:

msbuild buildscript.build /p:Configuration=Staging 

After the build, I don't see the web.config file transformed in the build artifacts folder. Is there something wrong here?

Thanks

like image 467
kind_robot Avatar asked May 08 '12 20:05

kind_robot


People also ask

How do I create a Web config transform?

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

What is Xdt transform replace?

A Transform attribute on a parent element can affect child elements even if no Transform is specified for them. For example, if you put the attribute xdt:Transform="Replace" in the system. web element, all the elements that are children of the system. web element are replaced with the content from the transform file.

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 you use Slowcheetah?

Step 1: Install the package "Slow Cheetah" into your solution. Step 2: Right click on the App. config and select the option Add Transform. Step 3: To tweak the appsetting values ,connection strings etc based on the configuration environement.


2 Answers

If you add the following xml to the bottom of the .csproj file for your web application, you'll ensure that the config transformation occurs before every build:

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" /> <Target Name="BeforeBuild">     <TransformXml Source="Web.Base.config" Transform="Web.$(Configuration).config" Destination="Web.config" /> </Target> 

Edit: In response to your comment, you should be able to use Web.config as the source parameter in the TransformXml task (see step #2). If you only want to perform the config transform in the build script, follow these instructions:

1) Import WebApplication.targets in your build script like so:

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" /> 

2) Execute the TransformXml build task in your build script target:

<Target Name="MyBuildScriptTarget">     <TransformXml Source="Web.config" Transform="Web.$(Configuration).config" Destination="Web.config" />     ...other build tasks... </Target> 
like image 83
Jonathan McIntire Avatar answered Sep 18 '22 21:09

Jonathan McIntire


Jonathan's answer is good. I tweaked it slightly to allow the original Web.config file to be retained. These are the lines I added to the bottom of my .csproj file:

  <!-- the step to copy the config file first avoids a 'File is being used by another process' error -->   <Target Name="BeforeBuild">     <Copy SourceFiles="Web.config" DestinationFiles="Web.temp.config" OverwriteReadOnlyFiles="True" />     <TransformXml Source="Web.temp.config" Transform="Web.$(Configuration).config" Destination="Web.config" />   </Target>   <Target Name="AfterBuild">     <Copy SourceFiles="Web.temp.config" DestinationFiles="Web.config" OverwriteReadOnlyFiles="True" />     <Delete Files="Web.temp.config" />   </Target> 

I can see that the web.config file is transformed by running a build within Visual Studio (or from a command line).

like image 37
David White Avatar answered Sep 20 '22 21:09

David White