views:

94

answers:

1

Hi,

My asp.net app has is using a web.config for common configuration. I also have a section that maps some data objects to connection strings, and that section is going to be couple thousand of lines. I want to move that section to another config file "dataMappings.config", so I don't bulk up web.config - is there a standard mechanism of accessing that config file?

Thank you, Andrey

+5  A: 

In each section, you can define configSource, which can point to an external file path. Here's a simple example:

<connectionStrings configSource="myConnectionStrings.Config" />
<appSettings configSource="myAppSettings.Config" />

Just make sure not to use .xml file extension since it can be viewed in a browser. .config will not be served by the web server.

Because your config sections are still defined in the web.config (thus pointing to external files), you can access this information via the normal routes (WebConfigurationManager.AppSettings, WebConfigurationManager.GetSection, ConfigurationManager, or custom section handlers as needed)

KP
Will it work if I am using a custom section? Or do I need to do something in that custom section code to support configSource attribute?
Andrey
+1 - Good answer, beat me to it!
Mark Brittingham
@Andrey: this will work with **any** `ConfigurationSection` in the .NET 2.0 and up framework.
marc_s
It should work. Here's another discussion on that point: http://stackoverflow.com/questions/398607/how-to-enable-configsource-attribute-for-custom-configuration-section-in-net
KP