我已经在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>
答案 0 :(得分:11)
默认情况下,管理转化的目标(TransformWebConfig
)仅适用于web.config
文件。
要使其适用于appSettings.config
文件,您必须:
Build Action
设置为Content
TransformWebConfig
和ProjectConfigFileName=appSettings.config
调用MSBuild目标Configuration=$(Configuration)
。要在转换TransformWebConfig
文件后立即调用appSettings.config
的{{1}}目标,您需要在项目文件的末尾添加它:
web.config
答案 1 :(得分:5)
让这更容易的东西,看看...... visualstudiogallery
上的SlowCheetah VS插件答案 2 :(得分: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>