Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

asp.net - Make Web.config transformations work locally

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

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?

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"

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

Here I manually transform the app.config file using transform.xml file and the destination file is app.prod.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...