转: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 对应的请求 ...
随机推荐
- paip.信用卡账单处理系统功能vO22
paip.信用卡账单处理系统功能vO22 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/att ...
- Spring简介和基础
Spring介绍 1.什么事Spring? spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架. spring的设计模式是单例模式和工厂模式. 2.spring的四大优点 轻量 ...
- Maven学习总结(二)——Maven项目构建过程练习_转载
上一篇只是简单介绍了一下maven入门的一些相关知识,这一篇主要是体验一下Maven高度自动化构建项目的过程 一.创建Maven项目 1.1.建立Hello项目 1.首先建立Hello项目,同时建立M ...
- muduo库的简单使用-echo服务的编写
muduo库的简单使用 muduo是一个基于事件驱动的非阻塞网络库,采用C++和Boost库编写. 它的使用方法很简单,参考这篇文章:TCP网络编程本质论 里面有这么几句: 我认为,TCP 网络编程最 ...
- Django基础——Model篇(一)
到目前为止,当程序涉及到数据库相关操作时,我们一般都会这么操作: (1)创建数据库,设计表结构和字段 (2)使用MySQLdb来连接数据库,并编写数据访问层代码 (3)业务逻辑层去调 ...
- CStringArray用法
CStringArray使用之前先设置数组尺寸SetSize,才能使用SetAt CStringArray m_strScrkRfid ; ...
- ThinkPHP执行原生sql,实现一些复杂的业务需求
1)事情起因:写php的同事做了社区消息接口,主要返回几个方面的消息,如我的主贴的点赞.我的层帖的点赞.我的主贴的评论.我的评论的评论, 数据因为关联了5张以上的表,返回的格式不一: 如原来的thin ...
- photoshop 快速切图
发现一个详细好方法:http://blog.csdn.net/zhangxiaowei_/article/details/42143307 具体如下:
- 转:主流数据恢复软件——EasyRecovery/Ashampoo Undeleter/Wise Data Recovery/Recuva/Undelete 360
转自:Baidu 空间 2012-10-05 13:57 主流数据恢复软件——EasyRecovery/Ashampoo Undeleter/Wise Data Recovery/Recuva/Und ...
- Linux下面使用rpm命令
RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的“添加/删除程序” rpm 执行安装包二进制包(Binary)以及源代码包(Source)两种 ...