2

I want to edit a web application's config at runtime, specifically the file that is referenced by a configSource attribute. Whenever I try to open the file using the WebConfigurationManager, I always get the web.config, not rewrite.config.

Once I get it open, I want to edit it (e.g. clear out existing entries, add new entries) and save it back out to rewrite.config.

Am I better off just reading it in as a file (e.g. System.IO)

web.config

<system.webServer>
  <rewrite>
    <rules configSource="rewrite.config" />
  </rewrite>

rewrite.config

<rules>
  <rule name="rewrite1" stopProcessing="true">
    <match url="^contact-us.html$" />
    <action type="Rewrite" url="/contact-us" />
  </rule>
  <rule name="rewrite2" stopProcessing="true">
    <match url="^about-us.html$" />
    <action type="Rewrite" url="/about-us" />
  </rule>
</rules>
Eric
  • 1,882
  • 3
  • 16
  • 21
  • have you checked into using XPath to edit the config file at runtime also there are a few examples on StatckoverFlow that I have seen that would help you as we.. try a google search `C# Stackoverflow edit config file at runtime` – MethodMan Jun 18 '15 at 20:31
  • I was seeeing if I could use WebConfigurationManager. XPath is not significantly different than System.IO. There are a lot of tutorials and questions about reading/writing appSettings and connection strings, but I'm looking for editing the content in a file that's referenced by a configSource. I'm leaning towards System.IO (or XPath) unless there's benefit and ability to WebConfigurationManager. – Eric Jun 19 '15 at 14:26
  • look at this Stackoverflow previous Posting it has a good example on how to use the `WebConfigurationManager` with a few lines of code http://stackoverflow.com/questions/2260317/change-a-web-config-programmatically-with-c-sharp-net – MethodMan Jun 19 '15 at 15:12

0 Answers0