10

我已经在我的 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>
4

3 回答 3

11

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


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

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

要在文件转换后调用 MSBuildTransformWebConfig目标,您需要在项目文件的末尾添加: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>
于 2010-05-11T16:31:22.487 回答
5

让这变得更容易的东西,看看 SlowCheetah VS 插件在... visualstudiogallery

于 2011-08-25T12:17:46.577 回答
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>
于 2012-11-16T07:43:18.607 回答