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.

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

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

 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

 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

 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.

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

Web.config file.

 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.

转:Transform Web.Config when Deploying a Web Application Project的更多相关文章

  1. Web.config Transformation Syntax for Web Application Project Deployment

    Web.config Transformation Syntax for Web Application Project Deployment Other Versions   Updated: Ma ...

  2. 10 Things ASP.NET Developers Should Know About Web.config Inheritance and Overrides(转)

    10 Things ASP.NET Developers Should Know About Web.config Inheritance and Overrides Wednesday, Janua ...

  3. 关于Web.config的debug和release.config文件

    使用Web.Config Transformation配置灵活的配置文件 发布Asp.net程序的时候,开发环境和发布环境的Web.Config往往不同,比如connectionstring等.如果常 ...

  4. 一步步开发自己的博客 .NET版(11、Web.config文件的读取和修改)

    Web.config的读取 对于Web.config的读取大家都很属性了.平时我们用得比较多的就是appSettings节点下配置.如: 我们对应的代码是: = ConfigurationManage ...

  5. 配置子目录Web.config使其消除继承,用虚拟目录创建多个网站的方法

    来源:http://www.wtnzone.com/post/2011/02/20/Set-Web-Config-to-Turn-Inheritance-Off.aspx ASP.NET提供了强大的W ...

  6. Web.config配置详解

    一.认识Web.config文件 Web.config   文件是一个XML文本文件,它用来储存   ASP.NET   Web   应用程序的配置信息(如最常用的设置ASP.NET   Web   ...

  7. web.config 配置

    一.认识Web.config文件   Web.config 文件是一个xml文本文件,它用来储存 asp.NET Web 应用程序的配置信息(如最常用的设置asp.NET Web 应用程序的身份验证方 ...

  8. web.config中配置页面出错后跳转指定错误页面

    每当用户访问错误页面时,会出现不友好的404错误,所以为了防止这种不友好,我们在web.config中的<system.web>节点下配置 <customErrors>,在出现 ...

  9. 我没发现Mvc里的 web.config 有什么用。

    实验过程 由于 Mvc2+ 引入 Area ,导致文件夹结构发生变化. Mvc下的 web.config 所在的位置是: ~/Areas/MySystem/Views/Web.config 对应的请求 ...

随机推荐

  1. 理解Certificate、App Id、Identifiers 和 Provisioning Profile

    做真机测试的时候,按照网上的流程,走通了,当时没有注意各种证书等的意思.现在做消息推送,需要各种证书.APP ID信息,为了更好的理解这个过程,所以整理了网上关于证书等的相关资料.方便自己和有需要的朋 ...

  2. MapReduce之单词计数

    最近在看google那篇经典的MapReduce论文,中文版可以参考孟岩推荐的 mapreduce 中文版 中文翻译 论文中提到,MapReduce的编程模型就是: 计算利用一个输入key/value ...

  3. SQLite数据库增删改查操作

    一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库——SQLite,SQLite3支持NULL.INTEGER.REAL(浮点数字).TEXT(字符串 ...

  4. 通过MSSQl作业定时执行批处理BAT文件

    前言 有些时候,我们可能会需要定时执行一下批处理来达到一定的目的,比如Oracle数据库的定时备份,当然Oracle也可以通过Rman实现定时备份.我们大多数的时候是通过操作系统的计划任务实现定时执行 ...

  5. Android MultiDex兼容包怎么使用?

    在Android系统中安装应用的时候,需要对Dex进行优化,但由于其处理工具DexOpt的限制,导致其id的数目不能够超过65536个.而MultiDex兼容包的出现,就很好的解决了这个问题,它可以配 ...

  6. HDU 1711 Number Sequence (KMP)

    白书说这个是MP,没有对f 数组优化过,所以说KMP有点不准确 #include <stdio.h> int a,b; int T[1000010],P[10010];//从0开始存 in ...

  7. ASP.NET Web API模型验证以及异常处理方式

    ASP.NET Web API的模型验证与ASP.NET MVC一样,都使用System.ComponentModel.DataAnnotations. 具体来说,比如有:[Required(Erro ...

  8. 搞了个基于zookeeper的Leader/Follower切换Demo

    基于zookeeper写了个Leader选举类库demo,场景如下: 上图中的Program1..4可以部署在1台server上,也可以部署在多台server上,也可以是一个进程中的多个线程. 运行效 ...

  9. [开发笔记]-MarkDown语法

    马克飞象MarkDown在线编辑器 http://maxiang.info/?basic 1. H1--H6 相应数量的# 2. 底线形式 = H1(最高阶标题)和- H2(第二阶标题) 3. 段落和 ...

  10. BusyBox

    http://blog.csdn.net/a345017062/article/details/6250619