3

I have an asp.net application that sets the configSource attribute on the rewriteRules element in web.config to point to a separate config file:

<rewrite>
    <rules configSource="App_Data\Config\RewriteRules.config" />
</rewrite>

My web app makes edits to the RewriteRules.config file programmatically, but my web app does not pick up the configuration changes after the file is edited and saved.

I have tried calling HttpRuntime.UnloadAppDomain() after editing the file. This successfully restarts my app domain, but the changes in RewriteRules.config are still not picked up. I have tried adding RestartOnExternalChanges="true" to the rewrite element, but this is apparently not supported on the IIS rewrite module. I have also tried ConfigurationManager.RefreshSection("rewrite/rules") but this does not seem to have any effect. The only way I can get the changes to take effect is to edit and save the main web.config file, but I am trying to avoid doing this programmatically for security reasons.

I am confused as to why HttpRuntime.UnloadAppDomain() does not cause external config files to be re-read. Is this expected behavior? Does the config file cache somehow exist outside the bounds of the app domain? Is there any practical way to achieve what I am looking to do?

John
  • 772
  • 7
  • 17

2 Answers2

0

Dude, the problem with your case is, related configSection definition is not marked as restartOnExternalChanges="true" in definition. For example; we created a custom config section for storing application urls in an external file and we create a section definition in web.config file like

<section name="pageUrlFormats" type="Kahia.Web.Configuration.PageUrlFormats.PageUrlFormatsSection, Kahia.Web" restartOnExternalChanges="true" requirePermission="false" />

so that asp.net knows if any change occurs in related file:

<pageUrlFormats configSource="Config\PageUrlFormats.config" />

application domain restarts. This goes same for all config section definitions, including UrlRewrite module's definition.

What you have to do is, find definition of related module. In this scenario, it is at apphost.config at C:\Windows\system32\inetsrv\config\applicationHost.config

In that file, look for rule section definition, it starts like

<section name="rules"

You have to add restartOnExternalChanges="true" attribute to that config file.

Oğuzhan Kahyaoğlu
  • 1,727
  • 2
  • 15
  • 20
  • By the way, i tested it with UrlRewrite module and this is not working for it due to native implementation of that module. Look for the accepted answer at [link](https://forums.iis.net/t/1155629.aspx) – Oğuzhan Kahyaoğlu Jul 27 '16 at 13:01
0

IIS7 configuration system uses the same syntax as the .Net framework configuration system, but is a different implementation that has some behavior differences. The restartOnExternalChanges thing is a feature of the .Net framework configuration system that is not supported by the IIS7 configuration system. The url rewriter module uses the IIS7 configuration system.

Meh Man
  • 463
  • 1
  • 6
  • 22