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>

 

 

出处:https://www.cnblogs.com/junchu25/p/3891810.html

posted on   jack_Meng  阅读(77)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2020-10-12 在bat脚本if中多条件判断
2020-10-12 如何在批处理中使用FOR /F输出空行
2020-10-12 bat文件修改或替换文件内容
2020-10-12 如何查看并确定已安装的 .NET Framework 版本
2019-10-12 C#中的事件的订阅与发布
2019-10-12 c# Winform 开发分屏显示应用程序

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

支付宝打赏

主题色彩