代码之家  ›  专栏  ›  技术社区  ›  Wallace Breza

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

  •  10
  • Wallace Breza  · 技术社区  · 16 年前

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

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

    <ItemGroup>
        <Content Include="appSettings.Debug.config">
            <DependentUpon>appSettings.config</DependentUpon>
        </Content>
    </ItemGroup>
    
    3 回复  |  直到 14 年前
        1
  •  11
  •   Julien Hoarau    16 年前

    默认情况下,管理转换的目标( TransformWebConfig web.config 文件。


    appSettings.config

    • 设置 Build Action Content
    • 调用MSBuild目标 具有 ProjectConfigFileName=appSettings.config 和 Configuration=$(Configuration) .

    转换网络配置 目标 刚刚经历了 web.config文件

    <PropertyGroup>
      <!-- Name of your custom config file -->
      <ConfigFileName>appSettings.config</ConfigFileName>
    </PropertyGroup>
    
    <PropertyGroup>
      <!-- 
          This property is used to handle circular dependency between
          TransformWebConfig and our custom target TransformAppConfig
      -->
      <FirstRun Condition="$(FirstRun) == ''">true</FirstRun>
    </PropertyGroup>
    
    <!-- This target will be called one time after a call to TransformWebConfig -->
    <Target Name="TransformAppConfig" 
            AfterTargets="TransformWebConfig"
            Condition="$(FirstRun) == 'true'">
    
      <MSBuild Projects="$(MSBuildProjectFile)"
               Targets="TransformWebConfig"
               Properties="ProjectConfigFileName=$(ConfigFileName);
                           Configuration=$(Configuration);
                           FirstRun=false"/>
    </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>
    
        2
  •  5
  •   SteveC    15 年前

    让这更简单的东西,看看SlowCheetah VS插件在。。。 visualstudiogallery

        3
  •  3
  •   Andrew    13 年前

        <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>