Access to a web.config from another project

S

skneife

I have a Solution that contains a website and its web.config and
another project named testWebSite.
I need to access to the web.*config located in the website project
from testWebSite like this:
string dbCs =
ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
This code failed and generates an exception because (it seems) that
the web.config is available only in the folder where it is.

So what is the solution for Access to a web.config from another
project ?

Sam
 
M

Michael Nemtsev

extract the setting u need to access to separate config file and refer to
this file from both web.configs
 
M

Mohamad Elarabi

You can do this but my guess is that both sites need to be up and running and
the context of the first site should be trusted on the site you're trying to
read the config file from. If not you might have to specify different
parameters for access.

Here is the code in 2 lines:

system.configuration.configuration oConfig =
system.web.configuration.webConfigurationManager.openWebConfiguration("/",
"testWebSite") as system.configuration.configuration;

string dbCs =
oConfig.connectionStrings.connectionStrings.item["myConnectionString"];

perhaps a .toString might be needed or a .value etc. at the end. The
reference to "testWebSite" above should match the name of the site in IIS not
the project's name. That's why I'm not sure how this would work in debug mode.

Also the OpenWebConfiguration method has several other signatures, so in
case you need to pass in an identity token or a username/password you can
check these here
http://msdn2.microsoft.com/en-us/li...onfigurationmanager.openwebconfiguration.aspx

Generally, I'd advise you reevaluate the reason you're having to do this.
There may be a better solution to this. Unless you're only doing this for
testing.

Hope that helps. Good luck.
 
S

skneife

I need to add some precision:
The second project is not a webSite but a Library (dll) for testing).

Sam
 
M

Mohamad Elarabi

In that case you'd do the following 3 lines:

string pathToConfigFile = "c:\mydllprojectfolder\mydll.config";

system.configuration.configuration oConfig =
system.configuration.configurationManager.OpenExeConfiguration(pathToConfigFile) as system.configuration.configuration;

string dbCs =
oConfig.connectionStrings.connectionStrings.item["myConnectionString"].connectionString;

Again I'd like to urge you to reconsider the reason you're having to do
this. If you're doing this to get a dev database connection string or other
dev environment information you're doing too much work for something that
could be done within the website's config file in much less effort.

Let me know if this helps. Thanks,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top