从命令行运行web.config转换

32

大家好!

我想要在使用VS2010发布对话框以及命令行的情况下构建ASP.NET MVC 2项目。

对于命令行,我已经让以下内容运作:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe .\SolutionFolder\MyProject.csproj /p:Configuration=Release;DeployOnBuild=True;PackageAsSingleFile=False;outdir=c:\_OutputFolder\

我唯一的问题是Web.config转换没有应用(但已添加到WebDeploy包中)。我不使用WebDeploy。有没有办法应用Web.config转换?

谢谢!


可能是MSBuild脚本和VS2010发布应用Web.config转换的重复问题。 - Frédéric Hamidi
3个回答

25

到目前为止,似乎是最好的解决方案。 - artvolk
1
这个页面上的所有文档链接现在都失效了。 - Luke

20

这里是另一种方法,使用msbuild来转换Web.config文件:

http://sedodream.com/2010/04/26/ConfigTransformationsOutsideOfWebAppBuilds.aspx

在我的测试中,结果更好。基本上,您需要创建一个项目文件,只执行TransformXML任务:

<Project ToolsVersion="4.0" DefaultTargets="Demo" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="TransformXml"
         AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>

<Target Name="Demo">
    <TransformXml Source="app.config"
                  Transform="Transform.xml"
                  Destination="app.prod.config"/>
</Target>
</Project>

保存项目文件,然后应用转换,运行以下命令:

msbuild trans.proj /t:Demo

其中trans.proj是项目文件的名称,Demo是任务目标的名称。


4
这是一个非常简单的方法。将 "v10.0" 替换为 "v15.0",以便于 Visual Studio 2017 找到 dll 文件。 - Luke
这简直太神奇了。它甚至能为无法转换的键生成警告(例如,在变换文件中具有“替换”转换但在应用变换的文件中未找到的键)。 - Triynko
我无法在Visual Studio 2017 Community中使其工作。我遇到了这个错误:error MSB4062: 从程序集C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v15.0\Web\Microsoft.Web.Publishing.Tasks.dll中无法加载“TransformXml”任务。无法加载文件或程序集。 - FredPonch

5

值得一提的是,您也可以在PowerShell中使用Visual Studio正在使用的DLL:Microsoft.Web.XmlTransform.dll

有关PowerShell脚本,请参见:Web.Config transforms outside of Microsoft MSBuild?

为了加载DLL而不是复制它,在我的工作环境中,我会这样做(这样您就可以看到在哪里找到这个DLL):

if (Test-Path "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\Web\Microsoft.Web.XmlTransform.dll") {
    Add-Type -LiteralPath "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\Web\Microsoft.Web.XmlTransform.dll"
} elseif (Test-Path "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\Web\Microsoft.Web.XmlTransform.dll") {
    Add-Type -LiteralPath "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\Web\Microsoft.Web.XmlTransform.dll"
} else {
    throw [System.IO.FileNotFoundException] "Microsoft.Web.XmlTransform.dll not found."
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接