2

I need to declare multiple config sources for rewrite url's as the list of rewrites is very lengthy (+10000 rewrites).

This doesn't work :

  <rewrite>
    <rules configSource="App_Config\Rewrite\UrlRewrites1.config"></rules>
    <rules configSource="App_Config\Rewrite\UrlRewrites2.config"></rules>
  </rewrite>
</system.webServer>

With the following exception:

Config section 'system.webServer/rewrite/rules' already defined. Sections must only appear once per config file.

1 Answer 1

1

The "configSource" supports only one config file, so you can't have multiple config files by default. This goes alose for the node in the web.config.

I suggest you try to do it programmatically You can use ServerManager class to get access to website configuration. You will probably need to add a reference to Microsoft.Web.Administration. Read your config files and add each rule mannually.

Something like this:

using (ServerManager serverManager = new ServerManager())
{
   Configuration config = serverManager.GetWebConfiguration("Default Web Site");
   ConfigurationSection rulesSection = config.GetSection("system.webServer/rewrite/rules");
   ConfigurationElementCollection rulesCollection = rulesSection.GetCollection();

   ConfigurationElement ruleElement = rulesCollection.CreateElement("rule");
   ruleElement["name"] = @"rule";

   ConfigurationElement matchElement = ruleElement.GetChildElement("match");
   matchElement["url"] = @"foo\.htm";

   ConfigurationElement actionElement = ruleElement.GetChildElement("action");
   actionElement["type"] = @"Rewrite";
   actionElement["url"] = @"bar.htm";
   rulesCollection.Add(ruleElement);

   serverManager.CommitChanges();

}

There is also another option but I haven't tried it myself. You can write your own custom rewrite provider for url rewrite module. Here is a walkthrough how to do it: Custom Rewrite Provider Walkthrough

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.