使用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. windows下pycharm远程调试pyspark

    参考http://www.mamicode.com/info-detail-1523356.html1.远端执行:vi /etc/profile添加一行:PYTHONPATH=$SPARK_HOME/ ...

  2. Spring配置注解详解

  3. div自身高度、屏幕高度

    获取元素高度 scrollWidth    //显示当前元素的宽度 scrollHeight   //显示当前元素的高度 scrollLeft     //显示当前元素的左边距左侧的距离 scroll ...

  4. InstallShield -6109

    背景:C#项目打包生成时一直提示生成失败,消息号-6109, 查找了好多资料均未能解决,有说ActiveX问题,有说注册表问题,作了相应修改依然未果:后来翻来翻去看到有关User32.dll引用时失败 ...

  5. DotNetCore跨平台~服务总线_事件总线的重新设计

    理论闲话 之前在.netFramework平台用的好好的,可升级到.net core平台之后,由于不再需要二进制序列化,导致咱们的事件机制遇到了问题,之前大叔的事件一直是将处理程序序列化后进行存储的, ...

  6. PB程序源码文件结构 pbl文件 pbd文件

    最近公司给了一套PB的源码,一个8.0,一个9.0,让给一个客户做软件整合,之前只听过PB看过别人写代码,为了快速上手,了解了一下PB的文件,记录如下:pbl为pb源码文件 pbd为程序编译后的文件 ...

  7. RecyclerView-------之GridView模式加载更多

    随着RecyclerView的出现,Listview.GridView的使用率相对有些减少,废话不多说,先看一下效果: 代码如下: 1.自定义的RecyclerView(根据自己的需要) public ...

  8. Java 9 揭秘(13. Collection API 更新)

    Tips 做一个终身学习的人. 在本章中,主要介绍以下内容: 在JDK 9之前如何创建了不可变的list,set和map以及使用它们的问题. 如何使用JDK 9中的List接口的of()静态工厂方法创 ...

  9. serv-u 多域配置

    serv-U后,默认情况下可以通过在浏览器中输入http://IP 的方式登录到serv-u ftp 服务器控制台,也就是说serv-u在80端口监听,这样,如果想配置多个域或者是想在自己电脑上安装A ...

  10. html5 01 随记

    一  HTML 是一种制作网站的标记语言 二.HTML基本语法 HTML 标签 html标签是html中的最基本单位 也是最重要的部分 通常使用尖角号 开始"<"和结束&qu ...