如何使用新的 VS 2010 配置转换并将它们应用到其他 .config 文件? [英] How to use the new VS 2010 configuration transforms and apply them to other .config files?
问题描述
我已经在我的 web.config 中为我的 connectionStrings 等设置了一些配置转换.但是我已经将我的 web.config 的某些区域分离到单独的文件中,例如 appSettings.config.
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.
如何配置 Visual Studio 和 MSBuild 以对这些额外的配置文件执行配置转换?
How can I configure Visual Studio and MSBuild to perform config transformations on these additional config files?
我已经按照 web.config 的方法在我的 web 应用程序项目文件中将文件关联在一起,但不会自动应用转换.
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>
推荐答案
默认情况下,管理转换的目标 (TransformWebConfig
) 仅适用于 web.config
文件.
By default the target managing the transformation (TransformWebConfig
) works only on web.config
file.
要使其在您的 appSettings.config
文件上运行,您必须:
To make it work on your appSettings.config
file you'll have to :
- 将文件的
Build Action
设置为Content
- 使用
ProjectConfigFileName=appSettings.config
和Configuration=$(Configuration)
调用 MSBuild 目标TransformWebConfig
.
- Set the
Build Action
of your file toContent
- Call the MSBuild target
TransformWebConfig
withProjectConfigFileName=appSettings.config
andConfiguration=$(Configuration)
.
要在 web.config
文件转换后为 appSettings.config
调用 MSBuild TransformWebConfig
目标,您需要在项目文件结束:
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) ransformed\%(DestinationRelativePath)</TransformOutputFile>
<TransformScope>$(_PackageTempDir)\%(DestinationRelativePath)</TransformScope>
</_WebConfigsToAutoParmeterizeCS>
<_WebConfigsToAutoParmeterizeCSOuputFiles Include="@(_WebConfigsToAutoParmeterizeCS->'%(TransformOutputFile)')">
</_WebConfigsToAutoParmeterizeCSOuputFiles>
</ItemGroup>
</Target>
这篇关于如何使用新的 VS 2010 配置转换并将它们应用到其他 .config 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!