使用Web.Config Transformation配置灵活的配置文件

发布Asp.net程序的时候,开发环境和发布环境的Web.Config往往不同,比如connectionstring等。如果常常有发布的需求,就需要常常修改web.config文件,这往往是一件非常麻烦的事情。

Web.Config Transformation能够在不同的发布环境下,产生不同的web.config文件,非常方便和实用。

一、Web.Config Transformation

三、Web.Config Transformation具体语法

一. Web.Config Transformation

项目中有个默认的web.config, 还可以定义格式为web.[name].config文件, 这个配置文件定义的规则, 在发布的时候, 会对web.config文件进行修改。

默认项目中, 会创建Web.Debug.config和Web.Release.config文件,分别对应于Debug和Release环境。

二. 一个实际的例子

假如我们要常常发布到测试服务器上,测试服务器和开发时候的connectionstring是不同的,看看如何使用Web.Config Transformation来解决这个问题。

1. 添加Test配置

菜单Build->Configuration Manager, 就能看到如下的配置窗口, 添加一个新的配置Test.

2. 添加Test config Transformation文件

在web.confg上,点击右键,Add Config Transform, VS就会为刚刚新建的Test配置新增Transformation文件 Web.Test.config

3. 修改Web.Test.config文件

下面的Web.Test.config中能够替换web.config中的connectionstring, 关键是这一段

<add name="MyDB"
connectionString="Data Source=TestSQLServer;Initial Catalog=MyTestDB;Integrated Security=True"
xdt:Transform="Replace" xdt:Locator="Match(name)"/>

xdt:Transform="Replace", 指的是用来做替换操作

xdt:Locator="Match(name), 指的是匹配规则,这里是匹配name

意思是用Web.Test.config中的这个配置节用来替换web.config中name为MyDB的配置

<?xml version="1.0" encoding="utf-8"?>

<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
In the example below, the "SetAttributes" transform will change the value of
"connectionString" to use "ReleaseSQLServer" only when the "Match" locator
finds an attribute "name" that has a value of "MyDB".
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=TestSQLServer;Initial Catalog=MyTestDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
-->
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" providerName="System.Data.SqlClient"
xdt:Transform="Replace" xdt:Locator="Match(name)"/>
</connectionStrings>
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<!--
In the example below, the "Replace" transform will replace the entire
<customErrors> section of your web.config file.
Note that because there is only one customErrors section under the
<system.web> node, there is no need to use the "xdt:Locator" attribute.
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
-->
</system.web>
</configuration>

4. 检查发布的结果

选择在Test配置下publish网站,你能看到最终的web.config文件,已经实现了替换connection string.

三. Web.Config Transformation具体语法

参考博客 http://www.cnblogs.com/worksguo/archive/2009/08/29/1556307.html

1 :locator属性

下面有个表,来详细列举locator的语法

(1)Match;

这里你需要就是在你直接匹配的属性名。

<connectionStrings>
<add name="Northwind" connectionString="connection string detail"
providerName="System.Data.SqlClient"
xdt:Transform="Replace"
xdt:Locator="Match(name)" />
</connectionStrings>

Engine会再你的Web.config中找到匹配name为Norhwind的就用上面的配置文件图替换。 
(2)Condition 
基于XPath,在Locator中应用有逻辑性的判断表达式。

<connectionStrings>
<add name="Northwind"
connectionString="connection string detail"
providerName="System.Data.SqlClient"
xdt:Transform="Replace"
xdt:Locator="Condition(@name=’Northwind or @providerName=' System.Data.SqlClient')" />
</connectionStrings>

上面就是Name属性匹配‘Norhwind’的或providerName匹配System.Data.SqlClient的配置文件节点都会被替换。 
(3)XPath 
这个就是直接写XPath, http://www.w3.org/TR/xpath ,这里是XPath的标准

<location path="c:\MySite\Admin" >
<system.web xdt:Transform="Replace" xdt:Locator="XPath(//system.web)"> </system.web>
<location>

这里你会发现,这里可以写一些列的表达式。

2: Transform 属性

(1) Replace 
表示所有匹配的节点都是替换

<assemblies xdt:Transform="Replace">
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
</assemblies>

其实这里描述文件时web.release.config,将要替换的文件时Web.config . 
(2) Remove 
删除第一匹配的元素。

<assemblies xdt:Transform="Remove">
</assemblies>

(3)RemoveAll

<connectionStrings>
<add xdt:Transform="RemoveAll"/>
</connectionStrings>

(4)Insert

插入从父节点中插入,(authorization中插入<deny users="*" />)

<authorization>
<deny users="*" xdt:Transform="Insert"/>
</authorization>

(5)SetAttributes

<compilation  batch="false"
xdt:Transform="SetAttributes(batch)">
</compilation>

(6)RemoveAttributes 
删除出Attributes

<compilation xdt:Transform="RemoveAttributes(debug,batch)">
</compilation>

(7)InsertAfter (XPath) 
通过匹配 XPath的表达式的,找到节点,并子节点后面插入 XML

<authorization>
<deny users="AName" xdt:Transform="InsertAfter(/configuration/system.web/authorization/ allow[@roles='Admins']") />
</authorization>

(8)InsertBefore (XPath) 
通过匹配 XPath的表达式的,找到节点,并子节点前面插入 XML

<authorization>
<allow roles=" Admins" xdt:Transform="InsertBefore(/configuration/system.web/authorization/ deny[@users='*'])" />
</authorization>

(9)XSLT (filePath)

可以在外部定义 XSLT文件,来替换Web.cofig文件。

<appSettings xdt:Transform="XSLT(V:\MyProject\appSettings.xslt)">
</appSettings> http://www.tuicool.com/articles/eUnAve

关于Web.config的debug和release.config文件的更多相关文章

  1. 爱上MVC~Web.Config的Debug和Release版本介绍

    回到目录 对于web.config来说,我们不会陌生,主要对站点进行相关参数的配置,当它被修改后,IIS里对应的应用程序池会被重启,而对于config里的一些配置我们一般使用比较多的是数据连接串con ...

  2. 区分debug和release生成文件的名称

    通常我们编译工程按照debug和release区分,且明确在Debug版本的生成文件中加入d标记.譬如: HelloWorld.exe 一般是release的生成文件,而debug版叫:HelloWo ...

  3. 为Debug和Release分别设置Web.config

    需求:在开发asp.net应用程序时,往往想在debug和release环境下使用不同的配置,而web.config文件却只有一个 解决方案:可以在原来的web.config中写下debug环境下的配 ...

  4. Web.Debug.config和Web.Release.config设置xdt:Transform无效的解决办法

    在VS中右键网站>发布时xdt:Transform 生效.但是使用tfs的build时,build可以正常通过,但是web.release.config中的xdt:Transform 无效,其它 ...

  5. .NET 的 Debug 和 Release build 对执行速度的影响

    这篇文章发布于我的 github 博客:原文 在真正开始讨论之前先定义一下 Scope. 本文讨论的范围限于执行速度,内存占用什么的不在评估的范围之内. 本文不讨论算法:编译器带来的优化基本上属于底层 ...

  6. VC Debug和Release区别

    https://msdn.microsoft.com/en-us/library/xz7ttk5s.aspx   Optimizing Your Code Visual Studio 2015 The ...

  7. SpringCloud Config Bus webhook 只能刷新config server 不能刷新config client

    在 https://github.com/spring-cloud/spring-cloud-bus/issues/124 中有提到 版本 SpringCloud:Greenwich.RC1 原因 由 ...

  8. Vue微信自定义分享时安卓系统config:ok,ios系统config:invalid signature签名错误,或者安卓和ios二次分享时均config:ok但是分享无效的解决办法

    简述需求:要求指定页面可以进行微信自定义分享(自定义标题,描述,图片,链接),剩下的页面隐藏所有基础接口.二次分享依然可以正常使用,切换至其他页面也可以正常进行自定义分享. 这两天在做微信自定义分享的 ...

  9. DVWA----DVWA System error - config file not found. Copy config/config.inc.php.dist to config/config.inc.php and configure to your environment.

    DVWA简介:DVWA(Damn Vulnerable Web Application)是一个用来进行安全脆弱性鉴定的PHP/MySQL Web应用,旨在为安全专业人员测试自己的专业技能和工具提供合法 ...

随机推荐

  1. 【Android Developers Training】 45. 控制音频焦点

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  2. Ion-affix & Ion-stick 仿IOS悬浮列表插件

    Ion-affix & Ion-stick 仿IOS悬浮列表插件 Ion-affix 1.相关网页 Ion-affix 2.环境准备: 执行命令 bower install ion-affix ...

  3. linux虚拟机下解压文件

    pscp命令上传文件到linux虚拟机   项目开发过程中,经常需要从windows向linux服务器上传下载文件.下面简单介绍一下如何上传下载文件. 下载安装putty软件:https://pan. ...

  4. 使用Homebrew配置Java开发环境

    查询java brew cask search java 查看版本信息 brew cask info java 从官网下载并安装 JDK 8 brew cask install java 需要安装 J ...

  5. 简单总结几种常见web攻击手段及其防御方式

    web攻击手段有几种,本文简单介绍几种常见的攻击手段及其防御方式 XSS(跨站脚本攻击) CSRF(跨站请求伪造) SQL注入 DDOS XSS 概念 全称是跨站脚本攻击(Cross Site Scr ...

  6. xhr.readyState的就绪状态

    0:初始化,XMLHttpRequest对象还没有完成初始化 1:载入,XMLHttpRequest对象开始发送请求 2:载入完成,XMLHttpRequest对象的请求发送完成 3:解析,XMLHt ...

  7. RabbitMQ系列教程之六:远程过程调用(RPC)

    远程过程调用(Remote Proceddure call[RPC])(本实例都是使用的Net的客户端,使用C#编写)  在第二个教程中,我们学习了如何使用工作队列在多个工作实例之间分配耗时的任务.  ...

  8. WPF制作带明细的环形图标

    效果 明细用Popup实现的,录gif时,Popup显示不出来,不知道为什么,所以静态图凑合看吧 大体思路 图表使用Arc+Popup实现 图表分为两部分,一是环形部分,一是标注的明细部分. 环形部分 ...

  9. (转发)RequestDispatcher的include()方法和forward()方法的区别

    forward(): 该方法用于将请求从一个Servlet传递给服务器上的另外的Servlet.JSP页面或者是HTML文件. 在Servlet中,可以对请求做一个初步的处理,然后调用这个方法,将请求 ...

  10. Unreal Engine 4 Radiant UI 入门教程(零)在场景中摆放网页

    相关的学习资源: https://forums.unrealengine.com/showthread.php?12097-PLUGIN-RadiantUI-SDK-UIs-HUDs-Interact ...