转:Transform Web.Config when Deploying a Web Application Project

时间:2021-08-15 10:08:11

Introduction

One of the really cool features that are integrated with Visual Studio 2010 is Web.Config (XDT) transformations. ASP.NET Web.Config transformations are a great way to manage configurations in several environments. We can easily change a database connection string or other settings within our Web.config file when  deploying a Web Application Project. When we deploy a Web site, we often want some settings in the deployed application's Web.configfile to be different from the development Web.config file. For example, we might want to change the connection strings, security settings, active directory and network addresses, proxy settings, service endpoints and so on. Not only change the connection string or other settings but also insert new settings, remove existing settings and so on during deployment. Here is the Web.Config(XDT) transformations syntax documentation on MSDN.

Background

Web.config transformation file contains XML markup that specifies how to change the Web.config file when it is deployed.

    • It only works for web applications. So firstly we have to create a Web Application Project .
    • After creating  the project Right click on web.config and select Add Config Transforms.

转:Transform Web.Config when Deploying a Web Application Project

    • When you click Add Config Transforms - this will create the default web.debug.config and web.Release.configfiles.

转:Transform Web.Config when Deploying a Web Application Project

The root element of a transform file must specify the XML-Document-Transform namespace in its opening tag. I used some features form ML-Document-Transform that I needed.

Web.Release.Config

转:Transform Web.Config when Deploying a Web Application Project Collapse | Copy Code
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appsettings>
<add key="Email" value="mmoin56@yahoo.com"
xdt:locator="Match(key)" xdt:transform="RemoveAll">
<add key="EMailFrom" value="mmoin56@hotmail.com"/>
<add key="EMailCC" value="mmoin56@yahoo.com"/>
<add key="EMailBCC" value="mmoin56@gmail.com"
xdt:locator="Match(key)" xdt:transform="Insert"/>
</appsettings> <connectionstrings xdt:transform="Replace">
<add name="Release-Mode" connectionstring="data source=MOIN-PC;initial catalog=MasterCareEMR;
user id=sa;password=123" providername="System.Data.SqlClient"/>
</connectionstrings> <system.web>
<compilation xdt:transform="RemoveAttributes(debug)"/>
<compilation batch="false" xdt:transform="SetAttributes(batch)"/>
<authentication mode="Windows" xdt:transform="SetAttributes(mode)"/>
</system.web>
</configuration>

Web.Debug.Config

转:Transform Web.Config when Deploying a Web Application Project Collapse | Copy Code
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appsettings>
<add key="Email" value="mmoin56@yahoo.com"
xdt:locator="Match(key)" xdt:transform="RemoveAll"/>
<add key="EMailFrom" value="mmoin56@hotmail.com"/>
<add key="EMailCC" value="mmoin56@yahoo.com"/>
<add key="EMailBCC" value="mmoin56@gmail.com"
xdt:locator="Match(key)" xdt:transform="Insert"/>
</appsettings> <connectionstrings xdt:transform="Replace">
<add name="Debug-mode" connectionstring="data source=MOIN-PC;initial
catalog=MasterCareEMR; user id=sa;password=123" providername="System.Data.SqlClient"/>
</connectionstrings>
<system.web>
<compilation batch="false" xdt:transform="SetAttributes(batch)"/>
<authentication mode="Windows" xdt:transform="SetAttributes(mode)"/>
<customerrors mode="RemoteOnly" defaultredirect="GenericErrorPage.htm"
xdt:locator="Match(mode)" xdt:transform="Insert">
<error statuscode="403" redirect="NoAccess.htm"/>
<error statuscode="404" redirect="FileNotFound.htm"/>
</customerrors>
</system.web>
</configuration>

Remove all existing values by using xdt:Locator="Match(key)" xdt:Transform="RemoveAll", and thenxdt:Transform="Insert", this will ensure any existing values are removed and inserted with the new ones. Alsoxdt:Transform="Replace" will replace any existing elements. In addition we can use SetAttributes andRemoveAttributes to add new attributes or remove existing attributes.

Default Web.Config file

转:Transform Web.Config when Deploying a Web Application Project Collapse | Copy Code
<configuration>
<appsettings>
<add value="mmoin56@yahoo.com" key="Email"/>
<add value="mmoin56@hotmail.com" key="EMailFrom"/>
<add value="mmoin56@yahoo.com" key="EMailCC"/>
</appsettings> <connectionstrings>
<add providername="System.Data.SqlClient"
connectionstring="data source=MOIN-PC;initial catalog=MasterCareEMR;
user id=sa;password=123" name="MCEMRBPPConnectionString"/>
</connectionstrings> <system.web>
<compilation targetframework="4.0" batch="true" debug="true"/>
<authentication mode="Windows"/>
</system.web>
</configuration>

Now we will publish the Project with Release mode.

转:Transform Web.Config when Deploying a Web Application Project

After publishing the Project we notice that the Web.config file has changed with new settings and a new connection string.

Web.config file.

转:Transform Web.Config when Deploying a Web Application Project Collapse | Copy Code
<configuration>
<appsettings>
<add value="mmoin56@hotmail.com" key="EMailFrom"/>
<add value="mmoin56@yahoo.com" key="EMailCC"/>
<add value="mmoin56@gmail.com" key="EMailBCC"/>
</appsettings> <connectionstrings>
<add providername="System.Data.SqlClient" connectionstring="data source=MOIN-PC;initial
catalog=MasterCareEMR; user id=sa;password=123" name="Release-Mode"/>
</connectionstrings> <system.web>
<compilation targetframework="4.0" batch="false"/>
<authentication mode="Forms"/>
</system.web>
</configuration>

If we again publish with Debug mode we will notice that the Web.config has changed the correspondingWeb.Debug.config file configuration. Really it is a very interesting feature of VS2010.

Conclusion

There are many flexible options to transform Web.config files using the xdt:Transform and xdt:Locator elements and it is very easy. I hope this will make your life a little easier.