在 applicationHost.config 中是否有替代 configSource 的方法?

Is there any alternative to configSource in applicationHost.config?

ASP.NET 站点使用 IIS 中的 ARR(应用程序请求路由)进行负载平衡。对应的URL改写规则放在applicationHost.config.

有什么方法可以在新的配置文件中分离这条规则吗?不再支持标签 configSource。我阅读了有关 childSource 标签的信息,但它仅在部分中受支持。

这是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>

我敢打赌,您遇到的情况是您希望在 testing/local 开发和 production/deploy 场景之间使用不同的配置设置。

我通常使用配置转换来实现这一点,而且效果很好。是这样的:

您的 app.config 文件基本上变成了一个模板。对于给出的示例,您的示例可能如下所示:

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

然后,创建另一个文件,将其命名为app.local.config,如下所示:

<?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>
...

和另一个文件,名为 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>
...

您可以在此处找到转换文档:https://docs.microsoft.com/en-us/previous-versions/dd465326(v=vs.100)

VS 在转换文件时内置了一些规则,但 IIRC 仅适用于 web.configs。添加 FastKoala 将允许 app.config 转换并能够在构建时转换它们,https://marketplace.visualstudio.com/items?itemName=JonDaviswijitscom.FastKoala-WebAppconfigXMLtransforms