使用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" locatorfinds 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>
转载于:https://www.cnblogs.com/webenh/p/7691780.html
使用Web.Config Transformation配置灵活的配置文件相关推荐
- ASP.NET 4.0 新特性--Web.Config Transformation(原创)
. 系列文章 Web.Config Transformation详解,这部分内容比较简单,关键是用没有用过的问题,所以这里希望帮助大家实践一下. 一 概述: 在VS2010中引入在Config 文件中 ...
- appsettings 连接oracle数据库,web.config中配置数据库连接的两种方式(appSettings 与 connectionStrings)...
[预览] [转载博客]cnblogs.com/sunzhiyue/archive/2011/06/07/2074696.html 在网站开发中,数据库操作是经常要用到的操作,ASP.NET中一般做法是 ...
- Web.Config文件配置之连接默认错误页
在一些网站中,当网络地址发生错误时,通常会自动跳转到一个页面,并在该页面显示错误信息,此功能叶可以通过配置Web.Config文件配置实现.例如访问者在访问网站时出现错误,程序将跳转到默认页面erro ...
- web.config中配置数据库连接的两种方式
在网站开发中,数据库操作是经常要用到的操作,ASP.NET中一般做法是在web.config中配置数据库连接代码,然后在程序中调用数据库连接代码,这样做的好处就是当数据库连接代码需要改变的时候,我们只 ...
- 动易sitefactory 数据库 mysql msssql_动易SiteFactory配置文件(web.config)常用配置节解读...
配置声明节 复制代码代码如下: 这个节定义了,web.config文件中将要用到具体配置节点的名称,处理程序等. 从这里可以看出,siteFactory里采用.net 企业应用程序块(enterpri ...
- Web.Config文件配置之数据库连接配置
Web.Config文件以XML形式存在于ASP.NET应用程序中,是ASP.NET应用程序的配置文件,包含程序调试.会话和全球化设置等配置信息,可以直接用记事本打开进行编辑.下面通过实例说明如何配置 ...
- Web.config的配置
通常把容易变化的量都在web.config中进行配置: 在web.config中进行配置修改方便不用重新编译的使网站的后期维护更灵活. web.config中的配置采用 xml文档格式,严格区分大小写 ...
- IIS错误页面隐藏版本信息 - Web.Config customErrors配置
目录 背景 分析暴露原因 解决办法 扩展学习customErrors使用方法 customErrors元素配置结构 元素属性 Mod 属性选项 示例 参考文章 背景 项目现按照国网的要求,测试后发现系 ...
- Web.Config文件配置之限制上传文件大小和时间
在邮件发送系统或者其他一些传送文件的网站中,用户传送文件的大小是有限制的,因为这样不但可以节省服务器的空间,还可以提高传送文件的速度.下面介绍如何在Web.Config文件中配置限制上传文件大小与时间 ...
最新文章
- 第二百二十节,jQuery EasyUI,Slider(滑动条)组件
- R语言使用ggplot2包使用geom_boxplot函数绘制基础分组箱图(分组箱体框颜色自定义配置)实战
- 推荐系统的十个关键点
- android+主界面所有应用程序图标添加统一背景主题,Android 4.0替Launcher主界面所有应用程序图标添加统一背景主题...
- 机器学习——深度学习之编程工具、流行网络结构、卷积神经网络结构的应用
- 3.3栈与递归的实现
- 算法:线性时间选择_机器学习必修课!scikit-learn 支持向量机算法库使用小结
- SpringCloud Gateway 服务网关,限流
- oracle中断进程,中断ORACLE数据库关闭进程导致错误案例
- 实数系与实数定理(上)
- 5种Alexa世界排名作弊方式
- Java——Json语法介绍与使用(详细介绍)
- 使用JS获取当前地理位置方法汇总
- Specification动态查询
- 金属类的大宗商品创下新高,对冲基金疯狂买涨
- LeetCode-Python-275. H指数 II
- 【计算机毕业设计】324企业人事信息管理系统设计与实现
- 如何在海外高效的推广APP?海外推广产品该注意哪些点?
- ker矩阵是什么意思_用初等变换求逆矩阵的小小解释
- Red Hat Enterprise Linux 7 正式版发布