web.config file transform from command line

会有一股神秘感。 提交于 2019-12-17 17:39:28

问题


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


回答1:


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>



回答2:


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:


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



回答4:


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"

and once ran, you can verify on the server that transformation takes place.



来源:https://stackoverflow.com/questions/10506308/web-config-file-transform-from-command-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!