简体   繁体   中英

web.config Multiple config sources

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM