標籤: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檔案