[ ASP.NET MVC ] 如何更好的管理MVC项目的Web.Config

本文详细介绍了如何在Microsoft Visual Studio 2010及后续版本中利用Web.config转换语法,特别关注了如何在Web.Debug.Config与Web.Release.Config中通过XPath表达式、Locator特性和Transform特性进行配置文件的定制化转换,旨在提高Web应用程序的部署效率和灵活性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在编译发布的过程中,Microsoft Visual Studio 2010 及以后的版本 会以 Web.Config为基础,而后基于编译的模式(debug or release)对 Web.Debug.Config 和 Web.Release.Config 进行加载,最终生成对应后的配置文件

 

Step 0:在Web.Debug.Config 与 Web.Rlease.Config中添加 xml 命名空间支持(先引用)

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

 

Step 1:学习使用 Locator 特征语法

Locator="Condition(XPath expression)"

  <connectionStrings>
    <add name="DebugConnection1" connectionString="Data Source=DebugSQLServer;Initial Catalog=DebugDb;Integrated Security=True" providerName="newprovider"
       xdt:Locator="Condition(@name='oldname' or @providerName='oldprovider')"  xdt:Transform="Replace" />
  </connectionStrings>

 

Locator="Match(comma-delimited list of one or more attribute names)"

  <connectionStrings>
    <add name="DebugConnection1" connectionString="Data Source=DebugSQLServer;Initial Catalog=DebugDb;Integrated Security=True" providerName="newprovider"
         xdt:Locator="Match(name)"  xdt:Transform="Replace" />
  </connectionStrings>


Locator="XPath(XPath expression)"

<configuration xmlns:xdt="...">  
  <connectionStrings>
    <add name="DebugConnection1" connectionString="Data Source=DebugSQLServer;Initial Catalog=DebugDb;Integrated Security=True" providerName="newprovider"
         xdt:Locator="XPATH(configuration/connectionStrings[@name='DebugConnection1' or @providerName='System.Data.SqlClient'])" xdt:Transform="Replace" />
  </connectionStrings>
</configuration>

以上的XPath表达式是将当前Add元素 (configuration/connectionStrings) 的隐式 XPath 条件与显式指定的表达式组合起来的结果

 

:Locator  特性是可选的。如果未指定元素的Locator 特性,默认是这个元素将会找到第一个符合自上往下的结构的标签名的元素执行已设置的 Transform 特性。例子如下:

<system.web xdt:Transform="Replace">
    <customErrors defaultRedirect="GenericError.htm" mode="RemoteOnly">
    <error statusCode="500" redirect="InternalError.htm"/>
   </customErrors>
</system.web>

 

Step 2 学习使用 Transform 特征语法

转换描述
xdt:Transform=“Replace”替换第一个匹配Locator的节点元素
xdt:Transform=“Remove”清除第一个匹配Locator的节点元素
xdt:Transform=“RemoveAll”清除所有匹配的节点标签名的元素
xdt:Transform=“Insert”将指定的元素添加到同级元素的末尾
xdt:Transform=“SetAttributes(attributeNames)”找到指定的元素,并修改对应属性的值
xdt:Transform=“RemoveAttributes(attributeNames)”找到指定的元素,如果有指定的属性,清除属性
xdt:Transform=“InsertBefore(XPath)”在指定Xpath前插入指定元素
xdt:Transform=“InsertAfter(XPath)”在指定Xpath后插入指定元素

 

a. Locator 特性可置于父级元素中。那么其子元素的集合将自动适应已设置的Transform特性,默认是 if exist replace , else add .

b. 如果指定的元素只有 Locator 特性而没有指定任何的 Transform 特性,那么将不会发生任何 Transform特性的操作,小心与 a 混淆

c. 父元素上的 Transform 特性会影响其所有的子元素,即使没有为子元素指定任何 Transform 特性也是如此 , 故尽量不要在 父元素 中设置 Transform 特性

 

 

参考文献:

用于 Web 应用程序项目部署的 Web.config 转换语法

ASP.net 4.0 新特性:Web.Config Transformation

 

转载于:https://www.cnblogs.com/VincentDao/p/4301887.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值