发布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

直接设置Attributes

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

使用Web.Config Transformation配置灵活的配置文件的更多相关文章

  1. Web.Config Transformation配置灵活的配置文件

    使用Web.Config Transformation配置灵活的配置文件 发布Asp.net程序的时候,开发环境和发布环境的Web.Config往往不同,比如connectionstring等.如果常 ...

  2. membership 在web.config中配置信息

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

  3. HttpModule在Web.config的配置和动态配置

    学习笔记 ASP.Net处理Http Request时,使用Pipeline(管道)方式,由各个HttpModule对请求进行处理,然后到达 HttpHandler,HttpHandler处理完之后, ...

  4. web.config中配置数据库(多数据)连接的两种方式

    这是我的第一篇文章,既然是第一篇了,那就从最基础的只是说起--web.config中配置数据库连接. 网上有很多这方面的资料,但发现并没有一篇从头到位很清楚明了说完的,今天就把我的整理写在这里吧. 在 ...

  5. c# MVC在WEB.Config中配置MIME

    在IIS中,默认没有添加.json格式的MIME,所有无法读取服务器中的.json格式的文件,返回结果404 方式一:在IIS中手动添加MIME 1.点击MIME进入MIME列表 2.添加MIME 3 ...

  6. c#与vb.net在App_Code里面编译要通过,需要以下web.config的配置

    web.config的配置: <system.web> <codeSubDirectories> <add directoryName="VB"/&g ...

  7. asp.net 多个域名重定向,在web.Config中配置

    一个网站有多个域名,但是需要在访问其中某个域名之后跳转到另一域名. Web.config 中配置 </system.webServer> <!--重定向 域名 开始--> &l ...

  8. 在Web.config中配置handler

    在Web.config中配置handler节点时发现用vs2010和用vs2015竟然不一样,经过多次测试发现了一些倪端: <configuration> <!--vs2010中需要 ...

  9. Web.config Transformation Syntax for Web Application Project Deployment

    Web.config Transformation Syntax for Web Application Project Deployment Other Versions   Updated: Ma ...

随机推荐

  1. C#/.NET 基础学习

    初识C# C#是微软公司发布的面向对象的.运行于.NET Framework之上的高级程序设计语言.与Java有着明显不同,借鉴Delphi的特点,与COM(组件对象模型)直接集成,是微软公司 .NE ...

  2. Oracle数据库,非空约束、主键约束、外键约束、唯一约束

    非空约束:设置列时,可为空默认可为空,去掉对号之后设置数据不可为空: 唯一约束:在键中设置,唯一约束名称.类型Unique.列名:设置应用完成之后,此列数据具有唯一性:即数据不可重复(类型:Uniqu ...

  3. 2016暑假多校联合---GCD

    Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). ...

  4. [moka同学笔记]Yii2 自定义class、自定义全局函数(摘录)

    1.在app\components下新建MyComponent.PHP namespace app\components; use Yii; use yii\base\Component; use y ...

  5. Spark集群 + Akka + Kafka + Scala 开发(3) : 开发一个Akka + Spark的应用

    前言 在Spark集群 + Akka + Kafka + Scala 开发(1) : 配置开发环境中,我们已经部署好了一个Spark的开发环境. 在Spark集群 + Akka + Kafka + S ...

  6. 使用PhpStrom等IDE编辑 远程linux服务器上的文件 实时更新

    习惯了在linux上 vim进行PHP的开发,突然忘了IDE的感觉,所以想到了.通过PhpStrom等IDE直接编辑 samba共享到 mac或者windows本地的磁盘进行编辑. 为了忘却的纪念,有 ...

  7. JProfiler 8下载地址和注册码

    JProfiler---- 一个很好的java性能监控工具,现在附上下载地址和注册码,此破解版仅供学习交流使用, 其他用途请购买正版授权!!!windows x64 zip下载地址:http://do ...

  8. Java继承中成员方法的overload(重载/过载)

    如果Java基础类有一个方法名被"过载"使用多次,在衍生类里对那个方法名的重新定义就不会隐藏任何基础类的版本.所以无论方法在这一级还是在一个基础类中定义,过载都会生效. publi ...

  9. 一种基于annotation的Spring-mvc权限控制方法

    简介 本文介绍一种采用annotation来对spring-mvc进行权限控制的方法. 通过枚举类来定义权限项. 将annotation标注到需要控制权限的spring-mvc方法上. 然后,在spr ...

  10. Android应用开发基础之十一:新特性

    Fragment 用途:在一个Activity里切换界面,切换界面时只切换Fragment里面的内容 生命周期方法跟Activity一致,可以理解把其为就是一个Activity 定义布局文件作为Fra ...