Moving <httpredirect> from web.config to a separate configuration file

We have many (over 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 it possible to manage this section in a separate web.config or any other best solution?

+4
source share
2 answers

You can move some configuration items to your own configuration file to reduce clutter in the web.config file.

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

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

And in your separate 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 just tried this with other configuration items.

+4
source

You can save this in the Separate Config file, as shown below: SectionInformation.ConfigSource Property

To avoid cluttering the configuration file - web.config - it can be defined in a separate configuration file. This file can then be referenced from the web.config file, as shown below:

 <httpRedirect configSource="httpRedirects.config" /> 

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

EDIT:

Please ensure that the httpRedirect attribute is set to enabled=true as the default value is false.

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

All Articles