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. paip.不同目录结构哈的文件批量比较

    paip.不同目录结构哈的文件批量比较 作者Attilax 艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/att ...

  2. mysql输入密码后闪退怎么办?

    第一: 首先需要想到的是mysql的服务可能没开,首先打开mysql的服务 第二: 打开Mysql的命令行输入密码即可 第三: 登录成功 第四: 顺便验证自己安装的mysql是否成功 输入显示所有数据 ...

  3. Maven系列--"maven-compiler-plugin"的使用

    maven是个项目管理工具,如果我们不告诉它我们的代码要使用什么样的jdk版本编译的话,它就会用maven-compiler-plugin默认的jdk版本来进行处理,这样就容易出现版本不匹配的问题,以 ...

  4. Android图片处理-图片压缩处理

    这里先重复温习一下上一篇,调用相册获取图片: /*** * 这个是调用android内置的intent,来过滤图片文件 ,同时也可以过滤其他的 */ Intent intent = new Inten ...

  5. APP顶号逻辑

    登录的接口login.do接口上需要记录关键的信息:userId.设备型号(Android|iPhone).登录时间.登录IP.设备唯一标识(UUID) 当在另外一台新设备上登录同一帐号时,将user ...

  6. JSP 处理汉字信息

    request 对象获取客户端提交的汉字字符时,会出现乱码问题,所以对含有汉字字符的信息必须进行特殊处理.将获取的字符串用 ISO-8859-1 进行编码,并将编码存放到一个字节数组中,再将这个数组转 ...

  7. C# inline-hook / api-hook

    我查阅了一下相关C#方面的资料,却没有发现有提供过关于api-hook方面的资 料包括应用库由此本人编写一套inline-hook的库用于支持x64.x86上的基于在 clr的公共语言,如: c#.c ...

  8. ab测试出现error: connection reset by peer的解决方案

    我们在使用一些开源程序之前,可能会使用ab工具在服务器或者本地进行一次性能评估,但是很多时候却总是会以失败告终,因为,服务器会拒绝你的ab工具发出的http请求, 出现 error: connecti ...

  9. HL AsySocket 服务开发框架 - 总体思路与架构

    一 背景 最近在园子了浏览了几篇有关Socket文章,得到了一些启发萌生了想要重构公司在2000年用.NET Framework 2.0 与 Visual Studio 2005开发的AsySocke ...

  10. [知识库分享系列] 三、Web(高性能Web站点建设)

    知识库分享系列: [知识库分享系列] 二..NET(ASP.NET) [知识库分享系列] 一.开篇 分享介绍此知识库之所以为 Web 系列,因为和 .NET 没有完全的关系,其中的技术和实践实用于各种 ...