简体   繁体   中英

Moving <httpredirect> out of web.config in separate config file

We have many (more than 100) redirects in our web.config like

<configuration>
   <system.webServer>
      <httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Found">
         <add wildcard="/a" destination="/a/dfdf/default.htm" />
         <add wildcard="/sad" destination="/aasd/dfdf/defsadault.htm" />
         <add wildcard="/asdsaa" destination="/aasdas/dfasddf/default.htm" />
         <add wildcard="/aasdsa" destination="/asdsaa/dfdf/defsdault.htm" />
         <add wildcard="/aasd" destination="/adsa/dfdf/default.htm" />
..... more than 100
      </httpRedirect>
   </system.webServer>
</configuration>

Is there way we can have this section managed in separate web.config or any other best solution?

You can move some config elements into their own config file to reduce clutter in the web.config.

<configuration>
   <system.webServer>
      <httpRedirect configSource="httpRedirects.config" />
   </system.webServer>
</configuration>

This is achieved by adding the configSource attribute as shown above.

And in your seperate httpRedirects.config

<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Found">
     <add wildcard="/a" destination="/a/dfdf/default.htm" />
     <add wildcard="/sad" destination="/aasd/dfdf/defsadault.htm" />
     <add wildcard="/asdsaa" destination="/aasdas/dfasddf/default.htm" />
     <add wildcard="/aasdsa" destination="/asdsaa/dfdf/defsdault.htm" />
     <add wildcard="/aasd" destination="/adsa/dfdf/default.htm" />
</httpRedirect>

Note I have only tried this with other config elements.

You can store that in Separate Config file as shown here: SectionInformation.ConfigSource Property

In order to avoid cluttering the configuration file - web.config - it can be defined in a separate configuration file. That file can then be referenced from the web.config file as below:

<httpRedirect configSource="httpRedirects.config" />

The configSource attribute tells IIS configuration that the <httpRedirect> section is defined in a separate file httpRedirects.config .

EDIT:

Please make sure you have httpRedirect attribute set to enabled=true as the default value is false.

<httpRedirect enabled="true" configSource="httpRedirects.config" />

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