. Net generates different config files based on configuration options.

Source: Internet
Author: User
Tags xslt

. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.