从命令行转换 web.config 文件 [英] web.config file transform from command line

查看:19
本文介绍了从命令行转换 web.config 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个构建环境可以定位;发布和分期.Web.config 如下所示:

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>

我想通过构建到暂存配置来转换它:Web.Staging.config

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

构建后,我没有看到构建工件文件夹中转换的 web.config 文件.这里有什么问题吗?

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

谢谢

推荐答案

如果您将以下 xml 添加到 Web 应用程序的 .csproj 文件的底部,您将确保在每次构建之前进行配置转换:

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)MicrosoftVisualStudiov10.0WebApplicationsMicrosoft.WebApplication.targets" />
<Target Name="BeforeBuild">
    <TransformXml Source="Web.Base.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
</Target>

响应您的评论,您应该能够使用 Web.config 作为 TransformXml 任务中的源参数(参见步骤 #2).如果您只想在构建脚本中执行配置转换,请按照以下说明操作:

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) 在构建脚本中导入 WebApplication.targets,如下所示:

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

<Import Project="$(MSBuildExtensionsPath32)MicrosoftVisualStudiov10.0WebApplicationsMicrosoft.WebApplication.targets" />

2) 在构建脚本目标中执行 TransformXml 构建任务:

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>

这篇关于从命令行转换 web.config 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆