38

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

0

5 Answers 5

40

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>
3
  • Thanks for the quick response. The problem with this is, I will need to maintain another config file: Web.base.config. When I build solution with VS, it will transform the Web.config which is not necessary. I want the transformation when I run the build script.
    – kind_robot
    Commented May 8, 2012 at 21:35
  • This works but one problem. When the transform happens, I get an error somewhat like this: Web.config file is being used by another process. I called this after the compilation target.
    – kind_robot
    Commented May 9, 2012 at 1:34
  • 1
    If you move your Web.config to another directory, you can change the Source parameter of TransfromXml to something like Source="ConfigDir\Web.config". If you do that, you'll probably want to perform the config transformation in the BeforeBuild target so that changes made to ConfigDir\Web.config are always copied to the root directory. The other option is to create a Web.Base.config file that you will have to maintain. Commented May 9, 2012 at 2:03
27

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

3
  • This answer is a lot better b/c when I was testing it changed my original web.config file in my VS project
    – Rob Scott
    Commented Jul 11, 2016 at 17:49
  • 3
    there is a mistake in the XML. In TransformXml you should swap Source and Destination. Otherwise transformation is not applied. Anyway thatnks for your answer a lot! after days of searching it seems to be the only correct answer in the Web Commented Sep 26, 2016 at 16:01
  • Excellent. Thanks. Really feels though we shouldn't need to do this (and haven't needed to before) but hey, it works... Commented Jun 29, 2017 at 7:52
11

Tiny improvement over Jonathan's answer:

Using this line below for importing the web targets will allow to be compatible with any Visual Studio version. Note this is not tied to version v10.0

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\WebApplications\Microsoft.WebApplication.targets" />
2

Using Build transform with Jenkins, I also see that web.config does not transform, however, the actual transformation takes place when you do the deploy. I use an all in one msbuild command to do the build and deploy together.

MSBuild MyProj.csproj /P:Configuration=Release /P:DeployOnBuild=True /P:DeployTarget=MsDeployPublish /P:MsDeployServiceUrl=https://your server/msdeploy.axd /P:AllowUntrustedCertificate=True /P:MSDeployPublishMethod=WMSvc /P:CreatePackageOnPublish=True /P:UserName=username /P:Password=password1 /P:DeployIISAppPath="Default Web Site or name of your website"

Once it has run you can verify on the server that transformation takes place.

1
  • Thanks for the detailed explanation @Sandeep about that the transformation takes place when you do the deploy, that helped me to understand and solve my problem.
    – vhugo
    Commented Feb 27, 2020 at 20:54
0

If you want to keep using the command line, add this to the command:

/p:UseWPP_CopyWebApplication=true /p:PipelineDependsOnBuild=false

Source

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.