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

发布于 2024-09-01 02:41:07 字数 422 浏览 3 评论 0原文

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

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

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

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

I have setup some configuration transforms in my web.config for my connectionStrings, etc. But I have separated out some areas of my web.config into separate files, ex) appSettings.config.

How can I configure Visual Studio and MSBuild to perform config transformations on these additional config files?

I have already followed the approach of the web.config to relate the files together within my web application project file, but transformations are not automatically applied.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何其悲哀 2024-09-08 02:41:08

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


要使其在您的 appSettings.config 文件上运行,您必须:

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

要在 web.config 文件转换后调用 appSettings.config 的 MSBuild TransformWebConfig 目标,您需要将其添加到您的项目文件:

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

By default the target managing the transformation (TransformWebConfig) works only on web.config file.


To make it work on your appSettings.config file you'll have to :

  • Set the Build Action of your file to Content
  • Call the MSBuild target TransformWebConfig with ProjectConfigFileName=appSettings.config and Configuration=$(Configuration).

To call MSBuild TransformWebConfig target for appSettings.config just after the transformation of web.config files, you need to add this at the end of your project file :

<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>
冷情 2024-09-08 02:41:08

要使这变得更容易,请查看 SlowCheetah VS 加载项,网址为 ... 视觉工作室画廊

Something that makes this a lot easier, take a look at the SlowCheetah VS add-in at ... visualstudiogallery

自此以后,行同陌路 2024-09-08 02:41:08

这是适合我的代码:

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

Here is the code that works for me:

    <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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文