转:Transform Web.Config when Deploying a Web Application Project
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
A 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

<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

<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

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

<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的更多相关文章
- Web.config Transformation Syntax for Web Application Project Deployment
Web.config Transformation Syntax for Web Application Project Deployment Other Versions Updated: Ma ...
- 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 ...
- 关于Web.config的debug和release.config文件
使用Web.Config Transformation配置灵活的配置文件 发布Asp.net程序的时候,开发环境和发布环境的Web.Config往往不同,比如connectionstring等.如果常 ...
- 一步步开发自己的博客 .NET版(11、Web.config文件的读取和修改)
Web.config的读取 对于Web.config的读取大家都很属性了.平时我们用得比较多的就是appSettings节点下配置.如: 我们对应的代码是: = ConfigurationManage ...
- 配置子目录Web.config使其消除继承,用虚拟目录创建多个网站的方法
来源:http://www.wtnzone.com/post/2011/02/20/Set-Web-Config-to-Turn-Inheritance-Off.aspx ASP.NET提供了强大的W ...
- Web.config配置详解
一.认识Web.config文件 Web.config 文件是一个XML文本文件,它用来储存 ASP.NET Web 应用程序的配置信息(如最常用的设置ASP.NET Web ...
- web.config 配置
一.认识Web.config文件 Web.config 文件是一个xml文本文件,它用来储存 asp.NET Web 应用程序的配置信息(如最常用的设置asp.NET Web 应用程序的身份验证方 ...
- web.config中配置页面出错后跳转指定错误页面
每当用户访问错误页面时,会出现不友好的404错误,所以为了防止这种不友好,我们在web.config中的<system.web>节点下配置 <customErrors>,在出现 ...
- 我没发现Mvc里的 web.config 有什么用。
实验过程 由于 Mvc2+ 引入 Area ,导致文件夹结构发生变化. Mvc下的 web.config 所在的位置是: ~/Areas/MySystem/Views/Web.config 对应的请求 ...
随机推荐
- Atitit.提升软件稳定性---基于数据库实现的持久化 循环队列 环形队列
Atitit.提升软件稳定性---基于数据库实现的持久化 循环队列 环形队列 1. 前言::选型(马) 1 2. 实现java.util.queue接口 1 3. 当前指针的2个实现方式 1 1.1 ...
- paip.web数据绑定 下拉框的api设计 选择框 uapi python .net java swing jsf总结
paip.web数据绑定 下拉框的api设计 选择框 uapi python .net java swing jsf总结 ====总结: 数据绑定下拉框,Uapi 1.最好的是默认绑定..Map(k ...
- PHP类与面向对象
PHP常量PHP常量大写PHP常量用define函数或const关键字定义一个常量一旦被定义,就不能再改变或者取消定义.在 PHP 5.3.0 以后,可以使用 const 关键字在类定义之外定义常量. ...
- Hadoop MapReduce例子-新版API多表连接Join之模仿订单配货
文章为作者原创,未经许可,禁止转载. -Sun Yat-sen University 冯兴伟 一. 项目简介: 电子商务的发展以及电商平台的多样化,类似于京东和天猫这种拥有过亿用户的在线购 ...
- java中static{}语句块详解
static{}(即static块),会在类被加载的时候执行且仅会被执行一次,一般用来初始化静态变量和调用静态方法,下面我们详细的讨论一下该语句块的特性及应用. 一.在程序的一次执行过程中,stati ...
- libuv在cocos2d-x中的使用
libuv经过Node.js的实践和应用,已经证明非常之成熟,本来之前项目用的是这个:clsocket https://github.com/DFHack/clsocket 当初选它的主要原因是它支 ...
- Revit如何设置尺寸标注的箭头样式
在尺寸标注类型属性中,有一名称为"记号标记"的属性,该属性控制线性标注的箭头样式,如图所示,可以从下"记号标记"下拉列表中选择需要的样式进行设置,但是有时候该下 ...
- apache 80端口部属多站点配置
1.在httpd.conf文件里启用虚拟主机功能,即去掉下面配置项前面的# #LoadModule vhost_alias_module modules/mod_vhost_alias.so 2..在 ...
- 源代码目录结构--AngularJS学习笔记(一)
最近开始接触AngularJS,确实是一个相当不错的东西,可以把很多东西简化掉.又对于其中的双向绑定等的实现很好奇,加之正在学习Javascript的东西,所以觉得从源代码这块开始深入学习Angula ...
- Mac上编译libimobiledevice库
0.准备工作: 使用brew或Mac Ports安装:libgnutls or openssl. libplist .libusb.libusbmuxd 1.下载代码: 下载地址:https://gi ...