. Net generates different config files based on configuration options.
Problems may occur during project development. The configuration of the development environment must be different from that of the production environment,
Manual copy is always repeated, but the disadvantages of too many copies are apparent,
There are several solutions to solve this problem:
1. Web. config Transformation
For more information about Transformation, see the following article,
This is a bad thing,It is executed only during publish and does not work during development and debugging,
Therefore, applications are generally used during the website release.
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 applies XslTransformation in the BuildBefore event
Sample Code: https://github.com/xlb378917466/MSBuild_BuildBefore
Learning knowledge: http://www.cnblogs.com/shanyou/p/3452938.html
This feature is very powerful. The BuildBefore event is used here, so that the modified configuration can be obtained during development and debugging,
<Target Name = "BeforeBuild">
<Strong transformation Condition = "'$ (Configuration) | $ (Platform)' = 'debug | AnyCPU '" XslInputPath = "Debug. xslt "XmlInputPaths =" WebTemplate. config "OutputPaths =" Web. config "/>
<Strong transformation Condition = "'$ (Configuration) | $ (Platform)' = 'release | AnyCPU '" XslInputPath = "Release. xslt "XmlInputPaths =" WebTemplate. config "OutputPaths =" Web. config "/>
</Target>
Two xslt files are defined here to output the final web. config file. Of course, you need to define an original input file WebTemplate. config,
In this example, the values in the simple receivetting are modified according to the actual 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. Use Symbols (Conditional compilation) to use C # code control
Refer to the previous article: Conditional compilation
This method is generally used only when loading other configurations such as XML. At least I used this method.