iisurl-rewritingapplicationhostconfigsource

Is there any alternative to configSource in applicationHost.config?


An ASP.NET site is load balanced using ARR (Application Request Routing) in IIS. The corresponding URL rewriting rule is placed in applicationHost.config.

Is there any way to separate this rule in a new config file? The tag configSource is no longer supported. I read about childSource tag but it is only supported in section.

Here is the rule in applicationHost.config:

<system.webServer>
        <rewrite>
            <globalRules>
                <rule name="ARR_TestFarm_loadbalance" patternSyntax="Wildcard" stopProcessing="true">
                    <match url="*" />
                    <action type="Rewrite" url="http://TestFarm/{R:0}" />
                </rule>
            </globalRules>
        </rewrite>
</system.webServer>

Solution

  • I'll bet what you have going on is a condition in which you want to have different config settings between testing/local development and a production/deploy scenario.

    I usually use config transforms to achieve this, and it works quite well. Goes something like this:

    Your app.config file basically becomes a template. For the example given, yours could look something like the following:

    ...
    <system.webServer>
            <rewrite>
                <globalRules>
                    <rule>
                    </rule>
                </globalRules>
            </rewrite>
    </system.webServer>
    ...
    

    Then, create another file, call it app.local.config it looks like this:

    <?xml version="1.0"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
        <system.webServer>
                <rewrite>
                    <globalRules>
                        <rule xdt:Transform="Replace">
                            <!-- local rule -->
                        </rule>
                    </globalRules>
                </rewrite>
        </system.webServer>
    </configuration>
    ...
    

    and another file, called app.release.config

    ...
    <system.webServer>
            <rewrite>
                <globalRules>
                    <rule xdt:Transform="Replace" name="ARR_TestFarm_loadbalance" patternSyntax="Wildcard" stopProcessing="true">
                        <match url="*" />
                        <action type="Rewrite" url="http://TestFarm/{R:0}" />
                </rule>
                </globalRules>
            </rewrite>
    </system.webServer>
    ...
    

    you can find the docs for transforms here: https://learn.microsoft.com/en-us/previous-versions/dd465326(v=vs.100)

    VS has some rules built in on when it transforms the files, but IIRC it is for web.configs only. The addition of FastKoala will allow for app.config transforms and the ability to transform them at build time, https://marketplace.visualstudio.com/items?itemName=JonDaviswijitscom.FastKoala-WebAppconfigXMLtransforms