. NET combat generates different config files based on configuration options

Source: Internet
Author: User
Tags xsl xslt

Project development process will encounter problems, development environment configuration is certainly not the same as the production environment,
Always repeat manual copy, but the drawbacks of configuring too many copies are revealed,
There are several scenarios to solve this problem:

1.web.config transformation

Transformation the relevant knowledge points can refer to the following article,

This thing has a bad place, it is only in the publish when the execution, during the development of debugging is not working,

So the general application during the site 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 application of Xsltransformation in Buildbefore event

Example code: Https://github.com/xlb378917466/MSBuild_BuildBefore

Knowledge Point Learning: http://www.cnblogs.com/shanyou/p/3452938.html

This is a powerful feature that uses the Buildbefore event so that you can get the modified configuration during development debugging.

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

This defines two XSLT files for outputting the final web. config file, and of course you have to define your own original input file Webtemplate.config,

In this example, the values in the simple appsetting 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-templatesSelect="@*|node ()"/> </xsl:copy> </xsl:template><xsl:template match="/configuration/appsettings/add[@key = ' Mode ')"> <add key="Mode"Value="Release"/></xsl:template></xsl:stylesheet>

3. Using C # code control through symbols (conditional compilation)

Refer to a previous article: conditional compilation

This is usually done when loading configurations such as other XML, at least for me at this time.

. NET combat generates different config files based on configuration options

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.