• Web.Config Transformation ASP.NET 4.0 新特性


    Web.Config Transformation详解,这部分内容比较简单,关键是用没有用过的问题,所以这里希望帮助大家实践一下。


    一 概述:

    在VS2010中引入在Config 文件中使用XML DOCUMENT TRANSFORM,这一个特性就是帮助你Web.config能方便的从部署配置文件转化到产品配置文件。协助Web.Config Transformation这两个功能就是web.debug.config, web.release.config等.这些文件的最后都会匹配到MSBuild中的配置文件去。

    其实是在web.debug.config和web.release.config来写一些描述文件,再通过Transformation Engine来转化。

    在Transformation Engine 执行下面任务

    首先是识别Locator属性是否设置,来判断是否使用XML转换,接着就是从原配置的XML文件中获得相应节点,再从转换的XML文件中招到适合Transform的值相匹配的节点,然后将他们转化到指定的XML配置文件。在转化中主要依赖的是Transform的attribute.



     

    二:实践




    基础部分:

    要使用XML-Document-Transform engine就要先引用XML-Document-Transform 命名空间,如果在你的Conifg文件中引用这个命名空间,你就能在本Web.config中使用转换描述。


    要使用XML-Document-Transform engine就要先引用XML-Document-Transform 命名空间,如果在你的Conifg文件中引用这个命名空间,你就能在本Web.config中使用转换描述。

    下面是在web.release.config中使用


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


    接下来是使用Locator,Locator是代表一组表达式,主要是基于 XPath的,通过配置Locator来查找Web.Config.并做相应的事情。

    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>


     

    总结(Summarize)


    ASP.NET 4.0中这个特性,主要能帮助大家的应用程序中的配置文件能从Debug平缓的转换到发布配置文件,而所以想的要从开发到产品的发布的转化,这只是其中的一部分,总的看这里主要就写一个描述文件,而描述文件如何和目标文件关联的,就是使用的Transform和Locator这两个属性来控制,再通过XML-Document-Tranformation engine来将他们粘合在一起。

    Transform和Locator都做下面这些事情:
    Transform :

    Replacing a node

    Inserting a node

    Delete a node

    Removing Attributes

    Setting Attributes

    Locator;

    Match on value of a node’s attribute

    Exact XPath of where to find a node

    A condition match to find a node

    最后别忘记, 他们是xdt扩展元素: xdt:Transform



    参考
    www.msdn.com
    http://weblogs.asp.net/gunnarpeipman/archive/2009/06/16/visual-studio-2010-web-config-transforms.aspx
    http://weblogs.asp.net/gunnarpeipman/archive/2009/06/16/visual-studio-2010-web-config-transforms.aspx

  • 相关阅读:
    pyqt5 动画在QThread线程中无法运行问题
    几种编码格式
    Android精品课程—PullToRefresh 下拉刷新
    【张泽华】android视频教程下载地址及上课源代码
    Android中常用适配器及定义自己的适配器
    VirtualBOX 虚拟机安装 OS X 10.9 Mavericks 及 Xcode 5,本人X220亲测
    google maps api申请的问题
    如何在Android应用程序中使用传感器(OpenIntents开源组织SensorSimulator项目)
    cvsnt 设置用户、修改密码
    Windows下用Git下载android源码 转载
  • 原文地址:https://www.cnblogs.com/chengjie/p/2360608.html
Copyright © 2020-2023  润新知