views:

1231

answers:

2

Hi,

I had some trouble on a test server with an ASP.NET website. I goofed, and had the home directory of the Default Web Site pointed to the wrong place. When I tried:

ConfigurationManager.ConnectionStrings["connectionString"]; 

it returned null, but

System.Configuration.Configuration rootWebConfig =        System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);     System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
rootWebConfig.ConnectionStrings.ConnectionStrings["connectionString"].ConnectionString;` 

returned the correct connection string.

What are all the differences between the two approaches?

EDIT: What I'm really asking is, why does the ConfigurationManager approach fail when the home directory is incorrectly set, but succeeds otherwise, and the WebConfigurationManager succeeds regardless of whether the home directory is correctly set? Are there any other differences, such as assumptions about access control?

A: 

First class provides access to general client config files (such as app.config) and second one for web application's files (such as web.config).

Restuta
+6  A: 
John K
Great answer so far. Why is it that the ConfigurationManager didn't find the web.config when the home directory was incorrect, whereas the WebConfigurationManager did? Does the ConfigurationManager start with the base web.config, then drill in, whereas the WebConfigurationManager goes straight to the virtual directory's web.config?
David Hodgson
I completely didn't answer your question and then added an edit to the top.
John K
I added information about how I reveal what directory and config file ConfigurationManager is trying to access, with results from my machine. Can you repeat on yours and see where your config file is coming from ...
John K
On a brand new web application project with one connection string, WebConfigurationManager.ConnectionStrings.ElementInformation.Source returns null, WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath).ConnectionStrings.ElementInformation.Source returns "C:\\Users\\David\\Documents\\Visual Studio 2008\\Projects\\WebConfigTests\\WebConfigTests\\web.config"; and ConfigurationManager.ConnectionStrings.ElementInformation.Source returns null even though ConfigurationManager.ConnectionStrings.Count is 2?
David Hodgson
What does this return for Source? `((ConfigurationSection) ConfigurationManager.GetSection("connectionStrings")).ElementInformation`
John K
I'm using that particular version because it tests the default init of `ConfigurationManager` instead of web manager. And it was the ConfigurationManager that originally gave you null connection string.
John K
((ConfigurationSection) ConfigurationManager.GetSection("connectionStrings")).ElementInformation.Source returns "C:\\Documents and Settings\\David\\My Documents\\Visual Studio 2008\\Projects\\WebConfigTests\\WebConfigTests\\web.config"
David Hodgson