Transformations in ASP.NET Web

Here i would like to explain Web Transformations in ASP.net. A transform file is an XML file that specifies how the Web.config file should be changed when it is deployed. Transformation actions are specified by using XML attributes that are defined in the XML-Document-Transform namespace, which is mapped to the xdt prefix

A transform file is an XML file that specifies how the Web.config file should be changed when it is deployed. Transformation actions are specified by using XML attributes that are defined in the XML-Document-Transform namespace, which is mapped to the xdt prefix. The XML-Document-Transform namespace defines two attributes: Locator andTransform.
• The Locator attribute specifies the Web.config element or set of elements that you want to change in some way. 
• The Transform attribute specifies what you want to do to the elements that the Locator attribute finds.
• The Locator and Transformelements themselves are not reproduced in the deployed Web.config file.
The following example shows the contents of a transform file that changes a connection string and replaces the customErrors element:




  
          connectionString="value for the deployed Web.config file" 
      xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
  
  
          mode="RemoteOnly" xdt:Transform="Replace">
      
    
  


Locator Attribute syntax
Condition
Syntax: Locator="Condition(XPath expression)"

The following example shows how to select connection string elements whose name attribute value is oldname or a providerName attribute whose value is oldprovider. In the deployed Web.config file, the selected elements are replaced with the element that is specified in the transform file.



  
           providerName="newprovider"
       xdt:Transform="Replace" 
       xdt:Locator="Condition(@name='oldname'
         or @providerName='oldprovider')" />
  

MATCH
Selects the element or elements that have a matching value for the specified attribute or attributes. If multiple attribute names are specified, only elements that match all the specified attributes are selected.
Syntax:Locator="Match(comma-delimited list of one or more attribute names)"

The following example shows how to select the connection string add element that has AWLT in the name attribute in the development Web.config file. In the deployed Web.config file, the selected element is replaced with the add element that is specified in the transform file.



  
           providerName="newprovider"
       xdt:Transform="Replace" 
       xdt:Locator="Match(name)" />
  

XPATH
Specifies an absolute XPath expression that is applied to the development Web.config file. (Unlike Condition, the expression that you specify is not appended to the implicit XPath expression that corresponds to the current element.)
Syntax:Locator="XPath(XPath expression)"

The following example shows how to select the same elements that are selected by the preceding example for the Condition keyword



  
           providerName="newprovider"
       xdt:Transform="Replace" 
       xdt:Locator="XPath(configuration/connectionStrings[@name='AWLT'
         or @providerName='System.Data.SqlClient'])" />
  


Transform Attribute syntax
Replace
Replaces the selected element with the element that is specified in the transform file. If more than one element is selected, only the first selected element is replaced. For an example of how to use the Replace keyword, see the examples for the Locator attributes.
Syntax: Transform="Replace"
SetAttributes
• Sets attributes for selected elements to the specified values. 
• The Replace transform attribute replaces an entire element including all of its attributes.
• In contrast, theSetAttributes attribute enables you to leave the element as it is but change selected attributes. 
• If you do not specify which attributes to change, all of the attributes that are present in the element in the transform file are changed.
NOTE :The SetAttributes transform affects all selected elements. This behavior is different from the Replace transform attribute, which affects only the first selected element if multiple elements are selected.
Syntax: Transform="SetAttributes(comma-delimited list of one or more attribute names)"
The following example shows how to select all the compilation elements in the development Web.config file. (Because there can be only one compilation element in the configuration file, you do not have to specify a Locator attribute.) In the deployed Web.config file, the value of the compilation element's batch attribute is set to false.



      batch="false"
    xdt:Transform="SetAttributes(batch)">
  


Insert
Adds the element that is defined in the transform file as a sibling to the selected element or elements. The new element is added at the end of any collection.
Syntax: Transform="Insert"
The following example shows how to select all the connection strings in the development Web.config file. In the deployed Web.config file, the specified connection string is added to the end of the collection.



  
           providerName="newprovider"
       xdt:Transform="Insert" />
  

Comments

Popular posts from this blog

Email Sending through O365 using OAuth Protocol

IISRESET vs App Pool Recycling ?

Deploy .Net6.0 Web api with docker