views:

65

answers:

2

We've got a somewhat complex project that the original developers split into multiple projects for organization purposes. What this means to me is that I've got an ASP.Net site that has a web.config and contains all sorts of good settings, and another project that compiles to a DLL that has it's own .config file, app.config.

In the DLL, it would be very nice to be able to access all the AppSettings that I have in the ASP.Net proejct's web.config file.

Is this possible? Is it good practice? I don't really want to copy and paste the same information and have to manually keep them in synch between the 2 (actually there might be up to 20 -- it's a large project and someone organized it to an absurd level) .config files.

Any suggestions or best practices?

+3  A: 

This is actually default behavior - ConfigurationManager.AppSettings read the current web.config for all DLLs.
Sometimes .config files are created for other projects (or dlls), but they are not used in the site and should be merged into web.config.

Kobi
Thank you so much! I was trying to test, but I was scared I would miss some odd combination of circumstances when deploying.
Matt Dawdy
A: 

System.Web.Configuration.WebConfigurationManager.AppSettings accesses the web.config from any class library. Just be sure to add a reference to System.Configuration and System.Web to any library needing to access this.

And to answer your question about standard pratice, yes it is common to store application key/values in the web.config to keep things centralized in ASP.NET web applications and web sites. In your web.config just use the <appSettings> section as you normally would in app.settings files.

KP
You're right, but why would you reference `System.Web`? You can just reference `System.Configuration`, and everything should be fine, eg. getting an app setting through `ConfigurationManager.AppSettings`.
klausbyskov
Fair enough. I tend to use the additional functionality found in WebConfigurationManager specific to web apps, so I prefer it. If simply finding name-value pairs is all the OP needs, ConfigurationManager may be the better solution...
KP
Good discussion of the two managers and their use:http://stackoverflow.com/questions/698157/whats-the-difference-between-the-webconfigurationmanager-and-the-configurationma/698163
KP