如何使用新的VS 2010配置转换并将其应用于其他.config文件?

时间:2010-05-11 14:31:33

标签: visual-studio-2010 msbuild web-config slowcheetah

我已经在web.config中为我的connectionStrings等设置了一些配置转换。但我已将web.config的某些区域分离为单独的文件,例如appSettings.config。

如何配置Visual Studio和MSBuild以对这些附加配置文件执行配置转换?

我已经按照web.config的方法在我的Web应用程序项目文件中将文件关联在一起,但不会自动应用转换。

<ItemGroup>
    <Content Include="appSettings.Debug.config">
        <DependentUpon>appSettings.config</DependentUpon>
    </Content>
</ItemGroup>

3 个答案:

答案 0 :(得分:11)

默认情况下,管理转化的目标(TransformWebConfig)仅适用于web.config文件。


要使其适用于appSettings.config文件,您必须:

  • 将文件的Build Action设置为Content
  • 使用TransformWebConfigProjectConfigFileName=appSettings.config调用MSBuild目标Configuration=$(Configuration)

要在转换TransformWebConfig文件后立即调用appSettings.config的{​​{1}}目标,您需要在项目文件的末尾添加它:

web.config

答案 1 :(得分:5)

让这更容易的东西,看看...... visualstudiogallery

上的SlowCheetah VS插件

答案 2 :(得分:3)

以下是适用于我的代码:

    <PropertyGroup>
    <!-- Name of your custom config file -->
    <ConfigFileName>ConnectionStrings.config</ConfigFileName>
    <ConfigTransformFileName>ConnectionStrings.$(Configuration).config</ConfigTransformFileName>
  </PropertyGroup>
  <PropertyGroup>
    <!-- 
      This property is used to handle circular dependency between
      TransformWebConfig and our custom target TransformAppConfig
  -->
    <FirstRun Condition="$(FirstRun) == ''">true</FirstRun>
  </PropertyGroup>
  <Target Name="AddConfigToTransform" AfterTargets="CollectWebConfigsToTransform">
    <ItemGroup>
      <WebConfigsToTransform Include="@(FilesForPackagingFromProject)" Condition="'%(FilesForPackagingFromProject.Filename)%(FilesForPackagingFromProject.Extension)'=='$(ConfigFileName)'">
        <TransformFile>%(RelativeDir)$(ConfigTransformFileName)</TransformFile>
        <TransformOriginalFile>$(TransformWebConfigIntermediateLocation)\original\%(DestinationRelativePath)</TransformOriginalFile>
        <TransformOutputFile>$(TransformWebConfigIntermediateLocation)\transformed\%(DestinationRelativePath)</TransformOutputFile>
        <TransformScope>$([System.IO.Path]::GetFullPath($(_PackageTempDir)\%(DestinationRelativePath)))</TransformScope>
      </WebConfigsToTransform>
    </ItemGroup>
  </Target>
  <!-- 
    This target will be called one time before PreAutoParameterizationWebConfigConnectionStrings 
    to add $(ConfigFileName) to autoparameterization step
-->
  <Target Name="AddToAutoParameterizationStep" BeforeTargets="PreAutoParameterizationWebConfigConnectionStrings">
    <ItemGroup>
      <_WebConfigsToAutoParmeterizeCS Include="@(FilesForPackagingFromProject)" Condition="('%(FilesForPackagingFromProject.Filename)%(FilesForPackagingFromProject.Extension)'=='$(ConfigFileName)') And !%(FilesForPackagingFromProject.Exclude)">
        <TransformOriginalFile>$(AutoParameterizationWebConfigConnectionStringsLocation)\original\%(DestinationRelativePath)</TransformOriginalFile>
        <TransformOutputFile>$(AutoParameterizationWebConfigConnectionStringsLocation)\transformed\%(DestinationRelativePath)</TransformOutputFile>
        <TransformScope>$(_PackageTempDir)\%(DestinationRelativePath)</TransformScope>
      </_WebConfigsToAutoParmeterizeCS>
      <_WebConfigsToAutoParmeterizeCSOuputFiles Include="@(_WebConfigsToAutoParmeterizeCS->'%(TransformOutputFile)')">
      </_WebConfigsToAutoParmeterizeCSOuputFiles>
    </ItemGroup>
  </Target>