Skip to main content

Microsoft

Web.config Transformation and XPath to update bindingRedirect

You can use Web.config transformations to update the bindingRedirect element in the web.config of an asp.net website.  This is more complex than other xdt transforms because the only identifying information is in a sibling assemblyIdentity element.  For example, the configuration for the default Sitecore System.Web.MVC dependentAssembly element looks like the following:

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" xmlns="urn:schemas-microsoft-com:asm.v1" />
        <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.1.0.0" xmlns="urn:schemas-microsoft-com:asm.v1" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

To change the bindingRedirect so we can use ASP.Net MVC version 5.2 in our Sitecore project, we have added the following to the Web.Debug.config:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1">
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly xdt:Transform="Replace" xdt:Locator="Condition(asmv1:assemblyIdentity/@name='System.Web.Mvc')">
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" xmlns="urn:schemas-microsoft-com:asm.v1" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.2.0" newVersion="5.2.2.0" xmlns="urn:schemas-microsoft-com:asm.v1" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

In the configuration above, the xdt:Locator finds a dependentAssembly element with a child, assemblyIdentity, whose name attribute equals System.Web.Mvc.  If found, the element’s contents are replaced with the new System.Web.Mvc version above.
Notice that the namespace for assemblyIdentity had to be added to the configuration node in the Web.Debug.config.  This namespace is then used in the condition statement.
This same technique can be used anytime you need to transform a node based on the value of a sibling’s attribute.
You may not need to use this technique often but it helps to keep custom modifications outside of the Sitecore configuration files when you are upgrading your site.  This way you can just replace your Sitecore configs with the new versions and check that the transforms are still needed.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Andrew Fitzpatrick

More from this Author

Categories
Follow Us
TwitterLinkedinFacebookYoutubeInstagram