replace rules values in web.config file

66 Views Asked by At

My web.config file is like below:

<rewrite>
  <rules>
    <rule name="GETDOCREDIRECT" stopProcessing="true">
      <match url="(.*)"/>
      <conditions>
        <add input="{REQUEST_URI}" pattern="xyz...."/>
      </conditions>
      <action type="Redirect" url="https://dev.azure.com" appendQueryString="false" 
    redirectType="Permanent"/>
    </rule>
    <rule name="Server_URL" stopProcessing="true">
      <match url="^xyz...../>
      <action type="Rewrite" url="/"^xyz.....//>
    </rule>
  </rules>
</rewrite>

==============================

In the above example, URL can be changed based on the environments.. can anyone let me know how to change web.release.config file to replace those values(file&variable transformation) similar like appsettings and connection strings.

1

There are 1 best solutions below

2
On

You can replace the entire rule using XDT:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <rewrite>
    <rules>
      <rule name="GETDOCREDIRECT" stopProcessing="true" xdt:Transform="Replace" xdt:Locator="Match(name)">
        <match url="(.*)"/>
        <conditions>
          <add input="{REQUEST_URI}" pattern="xyz...."/>
        </conditions>
        <action type="Redirect" url="https://prod.azure.com" appendQueryString="false" 
    redirectType="Permanent"/>
      </rule>
    </rules>
  </rewrite>
</configuration>

In this example, I'm replacing dev.azure.com with prod.azure.com as a theoretical example. Replacing the element is done by including the following attributes on the rule element you want to replace:

xdt:Transform="Replace" xdt:Locator="Match(name)"

That tells web.config transformations to replace the entire element with the element matching the value in the name attribute.

Source: Web.config transformations - The definitive syntax guide