关于Web.config的debug和release.config文件
使用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文件的更多相关文章
- 爱上MVC~Web.Config的Debug和Release版本介绍
回到目录 对于web.config来说,我们不会陌生,主要对站点进行相关参数的配置,当它被修改后,IIS里对应的应用程序池会被重启,而对于config里的一些配置我们一般使用比较多的是数据连接串con ...
- 区分debug和release生成文件的名称
通常我们编译工程按照debug和release区分,且明确在Debug版本的生成文件中加入d标记.譬如: HelloWorld.exe 一般是release的生成文件,而debug版叫:HelloWo ...
- 为Debug和Release分别设置Web.config
需求:在开发asp.net应用程序时,往往想在debug和release环境下使用不同的配置,而web.config文件却只有一个 解决方案:可以在原来的web.config中写下debug环境下的配 ...
- Web.Debug.config和Web.Release.config设置xdt:Transform无效的解决办法
在VS中右键网站>发布时xdt:Transform 生效.但是使用tfs的build时,build可以正常通过,但是web.release.config中的xdt:Transform 无效,其它 ...
- .NET 的 Debug 和 Release build 对执行速度的影响
这篇文章发布于我的 github 博客:原文 在真正开始讨论之前先定义一下 Scope. 本文讨论的范围限于执行速度,内存占用什么的不在评估的范围之内. 本文不讨论算法:编译器带来的优化基本上属于底层 ...
- VC Debug和Release区别
https://msdn.microsoft.com/en-us/library/xz7ttk5s.aspx Optimizing Your Code Visual Studio 2015 The ...
- SpringCloud Config Bus webhook 只能刷新config server 不能刷新config client
在 https://github.com/spring-cloud/spring-cloud-bus/issues/124 中有提到 版本 SpringCloud:Greenwich.RC1 原因 由 ...
- Vue微信自定义分享时安卓系统config:ok,ios系统config:invalid signature签名错误,或者安卓和ios二次分享时均config:ok但是分享无效的解决办法
简述需求:要求指定页面可以进行微信自定义分享(自定义标题,描述,图片,链接),剩下的页面隐藏所有基础接口.二次分享依然可以正常使用,切换至其他页面也可以正常进行自定义分享. 这两天在做微信自定义分享的 ...
- 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应用,旨在为安全专业人员测试自己的专业技能和工具提供合法 ...
随机推荐
- 查找oracle自己用户的表
查找oracle自己用户的表 select table_name from user_tables;
- 1000行代码徒手写正则表达式引擎【1】--JAVA中正则表达式的使用
简介: 本文是系列博客的第一篇,主要讲解和分析正则表达式规则以及JAVA中原生正则表达式引擎的使用.在后续的文章中会涉及基于NFA的正则表达式引擎内部的工作原理,并在此基础上用1000行左右的JAVA ...
- JavaScript学习笔记(一)——初识js
这个周,开始了JavaScript的学习路程.虽然从高中开始就接触了网页设计的知识,大学里的学习也算回顾了Html和Css的知识,实习期间在牛盾科技也是做的网站建设,不过通过前段时间找工作才发现自己了 ...
- VS2015 +Qt5 串口工具
简单的小工具是VS2015 + Qt5.6.1实现的,界面部分是Qt实现,串口是封装的WinAPI,把串口收发模块封装成了个Serialport.dll 供Qt界面调用. 由于VS2015需要CRT运 ...
- C#注册表操作类--完整优化版
using System; using System.Collections.Generic; using System.Text; using Microsoft.Win32; namespace ...
- 记一次redis挂机导致的服务雪崩事故~不对,是故事
事故时常有,最近特别多!但每次事故总会有人出来背锅!如果不是自己的锅,解决了对自己是一种成长,如果是自己的锅,恐怕锅大了,就得走人了,哈哈哈... 这不,最近又出了一个锅:从周五开始,每天到11点就不 ...
- UNITY VR 视频/图片 开发心得(一)
现在的VR似乎没有之前那么火热了,于是乎我居然开始了VR征程... 说起VR,对于没有接受过相关知识的人来说可能看起来比较高大上,但是VR的原理却没有想象中那么复杂.总的来说,VR之所以能够产生立体感 ...
- 第二章:2.8 通过Django 在web页面上面输出 “Hello word ”
1. 第一步:配置 guest 目录下面的 settings.py 文件, 将 sign应用添加到 guest项目中. 2. 在 guest目录下面,打开 urls.py 文件,添加 要打开的路由文件 ...
- ES6简介
function fn(){ return 100; } let name='sui'; let age=19; let sui={ name, age, ["pro"+fn()] ...
- spring boot controller路由 url 扫描不到问题
spring boot项目出现controller的路由没被注册,原因:启动类application跟controller不在一个包中,扫描不到controller, 如启动类在com.oyx.a,c ...