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

Wal*_*eza 10 msbuild web-config visual-studio-2010 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>
Run Code Online (Sandbox Code Playgroud)

Jul*_*rau 11

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


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

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

要在文件转换后调用MSBuild TransformWebConfig目标,您需要在项目文件的末尾添加它:appSettings.configweb.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>
Run Code Online (Sandbox Code Playgroud)


Ste*_*veC 5

这是令这个轻松了许多,看看在SlowCheetah VS附加在... visualstudiogallery