使Web.config转换在本地工作 [英] Make Web.config transformations work locally

查看:106
本文介绍了使Web.config转换在本地工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让web.config转换在本地工作,但是显然转换仅在进行部署时发生.

I want to get web.config transformations working locally but apparently the transformations only occur when doing deployments.

是否有人知道一种运行msbuild目标"TransformWebConfig"的方法,而无需经过重建"过程,也无需指定并输出将转换后的web.config吐出的目录?

Does anybody know of a way to run the msbuild target "TransformWebConfig" without it going through the "rebuild" process and also specify and output directory where to spit out the transformed web.config?

编辑:使用赛义德的回答,我创建了一个.bat文件来为我运行任务:

EDIT: Using Sayed's answer, I created a .bat file to do run the task for me:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Msbuild.exe "D:\Demo\Transformation.proj" /t:TransformWebConfig 

copy /Y  "D:\Demo\Web.config" "D:\MyProject\Web.config" 

del ""D:\Demo\Web.config"

"Transformation.proj"是下面答案中Sayed的代码段的副本.只需指定转换的源,目标和目标即可.在这种情况下,新文件转换后的"web.config"将位于"D:\ Demo"目录中.我只是将其复制以覆盖项目的web.config,最后删除"demo"文件夹中的生成文件.

the "Transformation.proj" is a copy of Sayed's code snippet in the answer below. Just specify the source, target, and destination for the transformation. The new file, in this case, the transformed "web.config" will be in the "D:\Demo" directory. I am simply copying it over to overwrite my project's web.config and, finally, deleting the generated file in the "demo" folder.

此外,我创建了一个宏来运行此批处理文件并为我执行转换:

Also, I created a macro to run this batch file and perform the tranformation for me:

Public Module DoTransform
    Sub RunTransformBatchFile()
        Try
          Process.Start("D:\Demo\RunTransform.bat")
        Catch ex As System.Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Module

您还可以在工具栏上添加一个按钮来运行该批处理和/或分配要执行的快捷键.

You can also add a button on your toolbar to run this batch and/or assign a shortcut key to execute.

推荐答案

如果要在不使用Web发布管道的情况下转换配置文件,则只需手动使用TransformXml任务即可.我已在 http://sedodream.com/2010/上写了一篇详细的博客文章. 04/26/ConfigTransformationsOutsideOfWebAppBuilds.aspx ,但以下是重点:

if you want to transform a config file without using the Web Publishing Pipeline then you just use the TransformXml task manually. I've written a detailed blog post on this at http://sedodream.com/2010/04/26/ConfigTransformationsOutsideOfWebAppBuilds.aspx, but here are the high lights:

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

在这里,我使用transform.xml文件手动转换app.config文件,而目标文件是app.prod.config.

Here I manually transform the app.config file using transform.xml file and the destination file is app.prod.config.

您提到的一件事是能够在运行应用程序时在本地进行转换.之所以我们只对包/发布执行转换,是因为如果我们转换了web.config本身,那么下次您调试应用程序时,web.config就会再次转换.因此,例如,如果在web.debug.config中进行了转换以向config添加值,则第一次添加时一切正常,但是下次运行/调试应用程序时,它将再次添加.因此,最好避免这种情况.

One thing that you mentioned was being able to do transformation locally when running the app. The reason why we only perform the transform on package/publish is because if we transformed web.config itself then next time you debug your app the web.config gets transformed again. So for example if in your web.debug.config you have the transformation to add a value to config, everything is OK the first time you add that but then the next time your run/debug your app it will get added again. So it is best to avoid that.

这篇关于使Web.config转换在本地工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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