zoukankan      html  css  js  c++  java
  • [ ASP.NET MVC ] 如何更好的管理MVC项目的Web.Config

    在编译发布的过程中,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

    作者:文道
    出处:http://www.cnblogs.com/VincentDao
    关于作者:北漂猴子一枚
    本文版权归作者文道所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接
    如有问题,可以通过邮件my_interface@126.com联系我,非常感谢。

  • 相关阅读:
    leetcode38.外观数列(循环匹配)
    leetcode35.搜索插入位置(遍历并进行大小判断)
    leetcode28.实现strStr()(暴力拆解,双指针,KMP算法)
    JavaScript对象
    数组迭代
    数组的用法:
    数组
    while与do while 区别 for循环的简介及break和continue的区别
    for循环语句
    if.......else语句子
  • 原文地址:https://www.cnblogs.com/VincentDao/p/4301887.html
Copyright © 2011-2022 走看看