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
  1. <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  2. <appsettings>
  3. <add key="Email" value="mmoin56@yahoo.com"
  4. xdt:locator="Match(key)" xdt:transform="RemoveAll">
  5. <add key="EMailFrom" value="mmoin56@hotmail.com"/>
  6. <add key="EMailCC" value="mmoin56@yahoo.com"/>
  7. <add key="EMailBCC" value="mmoin56@gmail.com"
  8. xdt:locator="Match(key)" xdt:transform="Insert"/>
  9. </appsettings>
  10.  
  11. <connectionstrings xdt:transform="Replace">
  12. <add name="Release-Mode" connectionstring="data source=MOIN-PC;initial catalog=MasterCareEMR;
  13. user id=sa;password=123" providername="System.Data.SqlClient"/>
  14. </connectionstrings>
  15.  
  16. <system.web>
  17. <compilation xdt:transform="RemoveAttributes(debug)"/>
  18. <compilation batch="false" xdt:transform="SetAttributes(batch)"/>
  19. <authentication mode="Windows" xdt:transform="SetAttributes(mode)"/>
  20. </system.web>
  21. </configuration>

Web.Debug.Config

 Collapse | Copy Code
  1. <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  2. <appsettings>
  3. <add key="Email" value="mmoin56@yahoo.com"
  4. xdt:locator="Match(key)" xdt:transform="RemoveAll"/>
  5. <add key="EMailFrom" value="mmoin56@hotmail.com"/>
  6. <add key="EMailCC" value="mmoin56@yahoo.com"/>
  7. <add key="EMailBCC" value="mmoin56@gmail.com"
  8. xdt:locator="Match(key)" xdt:transform="Insert"/>
  9. </appsettings>
  10.  
  11. <connectionstrings xdt:transform="Replace">
  12. <add name="Debug-mode" connectionstring="data source=MOIN-PC;initial
  13. catalog=MasterCareEMR; user id=sa;password=123" providername="System.Data.SqlClient"/>
  14. </connectionstrings>
  15. <system.web>
  16. <compilation batch="false" xdt:transform="SetAttributes(batch)"/>
  17. <authentication mode="Windows" xdt:transform="SetAttributes(mode)"/>
  18. <customerrors mode="RemoteOnly" defaultredirect="GenericErrorPage.htm"
  19. xdt:locator="Match(mode)" xdt:transform="Insert">
  20. <error statuscode="403" redirect="NoAccess.htm"/>
  21. <error statuscode="404" redirect="FileNotFound.htm"/>
  22. </customerrors>
  23. </system.web>
  24. </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
  1. <configuration>
  2. <appsettings>
  3. <add value="mmoin56@yahoo.com" key="Email"/>
  4. <add value="mmoin56@hotmail.com" key="EMailFrom"/>
  5. <add value="mmoin56@yahoo.com" key="EMailCC"/>
  6. </appsettings>
  7.  
  8. <connectionstrings>
  9. <add providername="System.Data.SqlClient"
  10. connectionstring="data source=MOIN-PC;initial catalog=MasterCareEMR;
  11. user id=sa;password=123" name="MCEMRBPPConnectionString"/>
  12. </connectionstrings>
  13.  
  14. <system.web>
  15. <compilation targetframework="4.0" batch="true" debug="true"/>
  16. <authentication mode="Windows"/>
  17. </system.web>
  18. </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
  1. <configuration>
  2. <appsettings>
  3. <add value="mmoin56@hotmail.com" key="EMailFrom"/>
  4. <add value="mmoin56@yahoo.com" key="EMailCC"/>
  5. <add value="mmoin56@gmail.com" key="EMailBCC"/>
  6. </appsettings>
  7.  
  8. <connectionstrings>
  9. <add providername="System.Data.SqlClient" connectionstring="data source=MOIN-PC;initial
  10. catalog=MasterCareEMR; user id=sa;password=123" name="Release-Mode"/>
  11. </connectionstrings>
  12.  
  13. <system.web>
  14. <compilation targetframework="4.0" batch="false"/>
  15. <authentication mode="Forms"/>
  16. </system.web>
  17. </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. TF Boys (TensorFlow Boys ) 养成记(五)

    有了数据,有了网络结构,下面我们就来写 cifar10 的代码. 首先处理输入,在 /home/your_name/TensorFlow/cifar10/ 下建立 cifar10_input.py,输 ...

  2. Java集合——题目

    第一题 (Map)利用Map,完成下面的功能: 从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队.如果该 年没有举办世界杯,则输出:没有举办世界杯. 附:世界杯冠军以及对应的夺冠年 ...

  3. Android Studio 生成Release版,报Warning的解决办法

    转载请注明出处:http://www.cnblogs.com/cnwutianhao/p/6242227.html 请尊重知识产权!!! 同步更新到CSDN:http://blog.csdn.net/ ...

  4. SpringIOC&AOP

    Spring是为简化企业级系统开发而诞生的,Spring框架为J2EE应用常见的问题提供了简单.有效的解决方案,使用Spring,你可以用简单的POJO(Plain Old Java Object)来 ...

  5. python查询

    #coding=utf-8 import MySQLdb conn = MySQLdb.Connect(host = '127.0.0.1',port=3306,user='root',passwd= ...

  6. Android CountDownTimer倒计时器的使用

    http://blog.csdn.net/freesonhp/article/details/25904047 在平时我们编程的时候,经常会用到倒计时这个功能,很多人不知道Android已经帮封装好了 ...

  7. 【高德地图API】如何设置Icon的imageSize?

    在地图开发中,我们需要把标注换成各种各样的图片,以突显个性.    在高分辨率的手机下,图片尺寸需要压缩至一半,以保持图片清晰.让我们来看一看,应该如何实现. 有开发者为了改变图片的尺寸,直接写成了 ...

  8. 【Android】Android 移动应用数据到SD

    [Android]Android 移动应用数据到SD 在应用的menifest文件中指定就可以了,在 <manifest> 元素中包含android:installLocation 属性, ...

  9. java代码实现如何获取当前经纬度?(安卓的话可以用GPS取)

    import android.app.Activity; import android.os.Bundle; import android.location.*; import android.con ...

  10. C# 中 KeyPress 、KeyDown 和KeyPress的详细区别[转]

    研究了一下KeyDown,KeyPress 和KeyUp ,发现之间还是有点学问的. 让我们带着如下问题来说明,如果你看到这些问题你都知道,那么这篇文章你就当复习吧:) 1.这三个事件的顺序是怎么样的 ...