如何使用新的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)\transformed\%(DestinationRelativePath)</TransformOutputFile>
<TransformScope>$(_PackageTempDir)\%(DestinationRelativePath)</TransformScope>
</_WebConfigsToAutoParmeterizeCS>
<_WebConfigsToAutoParmeterizeCSOuputFiles Include="@(_WebConfigsToAutoParmeterizeCS->'%(TransformOutputFile)')">
</_WebConfigsToAutoParmeterizeCSOuputFiles>
</ItemGroup>
</Target>
这篇关于如何使用新的VS 2010配置转换并将其应用于其他.config文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!