简体   繁体   中英

Web.config Transforms

VS Publish works, but not MSBuild.

I'm no expert in MSBuild, but I'll go through the process.

Following this website,

https://msdn.microsoft.com/en-us/vs2010trainingcourse_webdevelopment_topic4.aspx

, I have went through Steps 1-4. When I run "Publish" in VS 2015, I chose "file system" for the method, and the web.config file was changed based on the "Configuration" chosen, no problem.

However, when running MSBuild,

C:\Program Files (x86)\MSBuild\14.0\Bin>msbuild.exe "C:\pathToProject\project.sln" /p:Configuration=XXXX /p:OutputPath="C:\pathToOutputFolder"

did not work. The web.config did NOT change.

Am I missing something else?

There is an MSBuild task called TransformXml that the Publish menu item is executing, but is not getting executed from the command line. It's not part of a project file, and you have to execute it manually.

I have a build server that runs a custom MSBuild script to deploy a site, and it executes the transform with this:

<!-- Transform the profile-specific .config file into Web.config, placing result in staging folder -->
<Target Name="TransformWebConfig" Condition=" '$(WebsiteTransformFile)' != '' ">
    <Error Condition="!Exists('$(WebsiteTransformFile)')" Text="Web configuration file $(WebsiteTransformFile) not found." />
    <Message Text="Transforming Web.config with $(WebsiteTransformFile)" />
    <TransformXml Source="Web.config"
            Transform="$(WebsiteTransformfile)"
            Destination="Web.transformed.config" />
    <Copy SourceFiles="Web.transformed.config" DestinationFiles="Web.config" />
    <Delete Files="Web.transformed.config" />
</Target>

The property $(WebsiteTransformFile) is my own property, defined elsewhere in the file, that corresponds to the configuration's file, like Web.release.config .

You could add that to your .csproj file, or put it in a separate file, and execute it manually. It might be best to do it manually, so that when you press F5 to run locally, you'll use your un-transformed web.config , and not the transformed version that you need to deploy on a server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM