1

When I create a deployment package web.config is changed, but I don't understand this part:

I have two web.config transformation files, web.debug.config and web.release.config.

Are these transformation files only available or working when we make a web deployment or make a deployment package? Are the web.config transformations not used when the project runs locally from visual studio (e.g. via IIS Express)?

plr108
  • 1,081
  • 8
  • 15
Allende
  • 1,462
  • 2
  • 21
  • 37

4 Answers4

4

You are correct.

Config transformations are applied when you deploy or run a deployment package.

They do not transform on compilation.

Oded
  • 463,167
  • 92
  • 837
  • 979
4

If you need the transformed config file during compilation, you can get it by editing the project file (.csproj) and adding the below code.

<Target Name="AfterBuild">
    <TransformXml Source="$(SolutionDir)WCFServices\Web.config" 
                  Transform="$(SolutionDir)WCFServices\Web.Release.config" 
                  Destination="$(OutDir)WebRelease.config" 
                  StackTrace="true" />
</Target>

Multiple TransformXml tags can be added to get all the required config files. Also, This can be done before or after build.

Charls
  • 289
  • 1
  • 9
1

You can invoke it using MSBuild and an extension called SlowCheetah.

Daniel A. White
  • 174,715
  • 42
  • 343
  • 413
1

There is an other VS extension called Configuration Transform good for this. If you don't want to install it, but to achieve this, just follow the examples shown in the demo solution to add different build config files and add some new MSBuild tasks in the project files. The download link for the demo solution can be found on the extension's Visual Studio Gallery webpage. This approach doesn't require any extra packages since MSBuild uses XSLT to do the XML tranformation.

Below are the MSBuild tasks added into a project file from the demo solution. In my case, when I followed it for a VS2015 ASP.NET MVC project, I didn't have to put <UsingTask TaskName="TransformXml" AssemblyFile=... in.

  <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
  <Target Name="AfterCompile" Condition="Exists('App.$(Configuration).config')">
    <!--Generate transformed app config in the intermediate directory-->
    <TransformXml Source="App.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="App.$(Configuration).config" />
    <!--Force build process to use the transformed configuration file from now on.-->
    <ItemGroup>
      <AppConfigWithTargetPath Remove="App.config" />
      <AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
        <TargetPath>$(TargetFileName).config</TargetPath>
      </AppConfigWithTargetPath>
    </ItemGroup>
  </Target>
  <!--Override After Publish to support ClickOnce AfterPublish. Target replaces the untransformed config file copied to the deployment directory with the transformed one.-->
  <Target Name="AfterPublish">
    <PropertyGroup>
      <DeployedConfig>$(_DeploymentApplicationDir)$(TargetName)$(TargetExt).config$(_DeploymentFileMappingExtension)</DeployedConfig>
    </PropertyGroup>
    <!--Publish copies the untransformed App.config to deployment directory so overwrite it-->
    <Copy Condition="Exists('$(DeployedConfig)')" SourceFiles="$(IntermediateOutputPath)$(TargetFileName).config" DestinationFiles="$(DeployedConfig)" />
  </Target>

Here is the way I applied in my .csproj file, quite simple:

  <Target Name="AfterBuild" Condition="Exists('Web.$(Configuration).config')">
    <Exec Command="attrib -R Web.config" />
    <TransformXml Source="Web.config" Transform="Web.$(Configuration).config" Destination="Web.config" StackTrace="true" />
  </Target>

Also there is a good post on this.

Further, for web.config transformation, Since VS2012 we can add a publish profile - Publish.pubxml (ProjectFolder/Properties/PublishProfiles/Publish.pubxml) to do a FileSystem publish, thus the web.config transformation will happen by default then. Below is a sample

  <?xml version="1.0" encoding="utf-8"?>
  <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
      <WebPublishMethod>FileSystem</WebPublishMethod>
      <SiteUrlToLaunchAfterPublish />
      <publishUrl Condition="$(OutDir) != ''">$(OutDir)\_PublishedWebsites\$(ProjectName)</publishUrl> <!-- For MSBuild -->
      <publishUrl Condition="$(OutDir) == ''">$(MSBuildThisFileDirectory)..\..\_PublishedWebsite\</publishUrl> <!-- For Visual Studio...cant use $(ProjectName) -->
      <DeleteExistingFiles>True</DeleteExistingFiles>
    </PropertyGroup>
  </Project>
cateyes
  • 2,994
  • 1
  • 20
  • 26