.net 實戰 根據configuration選項產生不同的config檔案

來源:互聯網
上載者:User

標籤:put   input   href   form   生產環境   hive   ref   開發   調試   

項目開發過程中都會遇到的問題,開發環境的配置肯定是和生產環境不一樣的,
一直都是重複手動拷貝,但是配置太多拷貝的弊端就顯現出來了,
為瞭解決這個問題可以有幾種方案:

1.Web.config Transformation

Transformation的相關知識點可以參考下面的文章,

這個東西有個不好的地方,就是只有在publish的時候才執行,在開發調試期間是不起作用的,

所以一般應用在網站發布期間

https://msdn.microsoft.com/en-us/library/dd465326(v=vs.110).aspx

http://www.cnblogs.com/worksguo/archive/2009/08/29/1556307.html


2.MSBuild 在BuildBefore事件中應用XslTransformation

 範例程式碼: https://github.com/xlb378917466/MSBuild_BuildBefore

知識點學習:http://www.cnblogs.com/shanyou/p/3452938.html

 

這個功能很強大,這裡使用了BuildBefore事件,這樣在開發調試期間就可以擷取到修改之後的配置,

    <Target Name="BeforeBuild">
    <XslTransformation Condition="‘$(Configuration)|$(Platform)‘ == ‘Debug|AnyCPU‘" XslInputPath="Debug.xslt" XmlInputPaths="WebTemplate.config" OutputPaths="Web.config" />
    <XslTransformation Condition="‘$(Configuration)|$(Platform)‘ == ‘Release|AnyCPU‘" XslInputPath="Release.xslt" XmlInputPaths="WebTemplate.config" OutputPaths="Web.config" />
  </Target>

這裡定義了兩個xslt檔案用來輸出最終的web.config檔案,當然你要自己定義一個原始的輸入檔案WebTemplate.config,

這個例子簡單的APPSetting中的值根據實際的Configuration進行修改

<appSettings>    <add key="Mode" value="Release" />  </appSettings>

 Debug.Xslt

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output indent="yes" /> <xsl:template match="@*|node()">   <xsl:copy>     <xsl:apply-templates select="@*|node()" />   </xsl:copy> </xsl:template><xsl:template match="/configuration/appSettings/add[@key=‘Mode‘]"> <add key="Mode" value="Debug"/></xsl:template></xsl:stylesheet>

 Release.Xslt

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output indent="yes" /> <xsl:template match="@*|node()">   <xsl:copy>     <xsl:apply-templates select="@*|node()" />   </xsl:copy> </xsl:template><xsl:template match="/configuration/appSettings/add[@key=‘Mode‘]"> <add key="Mode" value="Release"/></xsl:template></xsl:stylesheet>

 

 

3.通過Symbols(條件編譯)來使用C#代碼控制

  參考之前的一篇文章:條件編譯  

這種做法一般是在載入其他XML之類的配置時才用得到,至少我是這個時候用的

.net 實戰 根據configuration選項產生不同的config檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.