App.config使用ASP.NET Web Project的Transformation

1、创建对应configuration的App.config文件,比如:App.Debug.config、App.Release.config。

2、编辑项目文件,将App.*.config文件的Build Action修改为Content,参考如下:

<Content Include="App.config" />
    <Content Include="App.Debug.config">
      <DependentUpon>App.config</DependentUpon>
    </Content>
    <Content Include="App.Publish.config">
      <DependentUpon>App.config</DependentUpon>
    </Content>
    <Content Include="App.Release.config">
      <DependentUpon>App.config</DependentUpon>
    </Content>

3、在</Project>标签前加入以下配置:

<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
  <Target Name="AfterCompile" Condition="exists(‘app.$(Configuration).config‘)">
    <TransformXml Source="app.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="app.$(Configuration).config" />
    <ItemGroup>
      <AppConfigWithTargetPath Remove="app.config" />
      <AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
        <TargetPath>$(TargetFileName).config</TargetPath>
      </AppConfigWithTargetPath>
    </ItemGroup>
  </Target>

重新加载项目,就可以看到App.config和Web.config相同的效果。

如果不想使用各种xdt语法,可以直接指定configuration的App.config:

<PropertyGroup Condition=" ‘$(Configuration)|$(Platform)‘ == ‘Release|AnyCPU‘ ">
  <AppConfig>App.Release.config</AppConfig>
</PropertyGroup>

使用上下文变量修改为更通用的配置:

<PropertyGroup>
  <AppConfig>App.$(Configuration).config</AppConfig>
</PropertyGroup>

 

App.config使用ASP.NET Web Project的Transformation,布布扣,bubuko.com

App.config使用ASP.NET Web Project的Transformation

上一篇:Android_程序未处理异常的捕获与处理


下一篇:Android与WebView的插件管理机制