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

查看:25
本文介绍了使 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?

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

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

C:WindowsMicrosoft.NETFramework64v4.0.30319Msbuild.exe "D:DemoTransformation.proj" /t:TransformWebConfig 

copy /Y  "D:DemoWeb.config" "D:MyProjectWeb.config" 

del ""D:DemoWeb.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:DemoRunTransform.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)MicrosoftVisualStudiov10.0WebMicrosoft.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 中进行了向配置添加值的转换,则第一次添加时一切正常,但下次运行/调试应用程序时,它将再次添加.所以最好避免这种情况.

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天全站免登陆