Using configBuilders with system.webServer rewrite section

554 Views Asked by At

I use the IIS Url Rewrite module, which is configured under system.webServer/rewrite.

I'm trying to use a basic Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder configBuilder with mode="Expand" so I can inject an environment variable into my <rewrite> config section like so:

<configuration>
    <configSections>
        ...
        <section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" />
        ...
    </configSections>
    ...
    <configBuilders>
        <builders>
            <add name="MyConfigBuilder" mode="Expand" type="Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Environment, Version=1.0.0.0, Culture=neutral" />
        </builders>
    </configBuilders>
    ...
    <system.webServer>
        <rewrite configBuilders="MyConfigBuilder">
            <providers>
                <provider name="SomeProvider">
                    <settings>
                        <add key="someKey" value="${someEnvToken}" />
                    </settings>
                </provider>
            </providers>
            ...
        </rewrite>
    </sytem.webServer>
</configuration>

I get a 500 - Internal server error. There is a problem with the resource you are looking for, and it cannot be displayed. with no stack trace or any further information. Visual studio intellisense states The "configBuilders" attribute is not allowed which I guess explains the 500 error.

If I try at any other level in the rewrite config, e.g. on the child element <providers> or <provider> or <settings> then I get a 500 error with Unrecognized attribute 'configBuilders'

I've tried placing the configBuilders attribute on the <system.webServer> element itself, too, but there's still an error.

As far as I'm aware the attribute configBuilders needs to go on a configSection only. Which means system.webServer is out because it's a configSectionGroup. In theory, rewrite should be a configSection and therefore accept the attribute, no?

How do I get this to work with the url rewrite module? If it can't work, then I must ask: what's the point if all it can do is appSettings and connectionStrings? I'm aware you can create custom section handlers for your own custom sections. The problem is this isn't a custom section - it's a highly used module made by Microsoft and used by almost anyone running a website on IIS!

--- EDIT ---

I've since discovered that the <rewrite> element is also a configSectionGroup but all of the child elements are configSection so it should work in theory. I found this link:

https://githubmemory.com/repo/aspnet/MicrosoftConfigurationBuilders/issues/149

Where a guy is creating his own sectionHandler for a well-known section <customErrors> which seems to work. I believe it instructs the configBuilders to process the section whereas otherwise it ignored it.

The class is declared like so:

public class CustomErrorsSectionHandler : Microsoft.Configuration.ConfigurationBuilders.SectionHandler<CustomErrorsSection>

Note that CustomErrorsSection is in System.Web.Configuration and is public sealed.

I've tried to replicate this by creating a sectionHandler<Microsoft.Web.Management.Iis.Rewrite.ProvidersSection> but it's internal sealed so it's not possible to implement.

Is this literally impossible??

0

There are 0 best solutions below