.net随笔——Web开发config替换到正式config appSettings

2023-02-12,,,,

前言(废话)

  查了一些资料,总体来说呢,就是坑,而且顺带吐槽下百度,一个内容被copy那么多遍还排在最前面。同一个内容我点了那么多次,淦。

正题:

  实现目的:开发的时候使用system.debug.config这个配置文件

       发布的时候使用system.Release.config这个配置文件

我为啥这么干,因为公众号的临时token我存在这配置文件里面。每次发布都得清楚,贼鸡儿麻烦

 所以我用到了webconfig转换

--------------贴使用代码begin----------------------------------------

这是web.config里面的配置
<configuration>
<appSettings configSource="Config\system.Debug.config" />
</configuration> 注意:这里是web.release.config里面的配置
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings configSource="Config\system.Release.config" xdt:Transform="Replace" xdt:Locator="XPath(/configuration/appSettings)" />
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>
</configuration>

--------------贴使用代码end----------------------------------------

就这么完事,接下来是解释。

正如网上说的,发布的时候会自动替换

那么是怎么样的替换法:

  第一:关键属性xdt:Transform="Replace"。。。这个xdt:Transform很多用法主要作用就是操作,

      可以替换Replace、

      可以添加Insert、

      或者InsertBefore和InsertAfter、

      以及Remove与RemoveAll、

      RemoveAttributes(debug,batch)这个也不知道咋用的玩意、

      SetAttributes(batch)

      SetAttributes(viewStateEncryptionMode)

  第二:筛选xdt:Locator="XPath(/configuration/appSettings)"  这个条件的语法是:xdt:Locator="XPath(XPath Expression)" 。或者xdt:Locator="Condition(XPath Expression)" 进行某个属性的操作

    是XPathExpression表达式,这个可以参照这位老哥的 https://www.cnblogs.com/james-dong/articles/438825.html

.net随笔——Web开发config替换到正式config appSettings的相关教程结束。

《.net随笔——Web开发config替换到正式config appSettings.doc》

下载本文的Word格式文档,以方便收藏与打印。