Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between the WebConfigurationManager and the ConfigurationManager?

What's the difference between the WebConfigurationManager and the ConfigurationManager?

When should I use one over the other?

UPDATED

I just looked at the WebConfigurationManager, and for some reason, you can't access the connection strings as you do in the ConfigurationManager (like an array). Can anyone tell me why MS made it like this? It seems to be a pain to get the connection string you need using the WebConfigurationManager.

UPDATED AGAIN with CAVEAT!

If you don't have a reference to the System.Configuration namespace added to your project, then Visual Studio will show an error when you try and access the WebConfigurationManager.ConnectionStrings like an array!

like image 216
John B Avatar asked Mar 30 '09 17:03

John B


People also ask

What is ConfigurationManager configuration?

The ConfigurationManager class enables you to access machine, application, and user configuration information. This class replaces the ConfigurationSettings class, which is deprecated. For web applications, use the WebConfigurationManager class.

Does ConfigurationManager work in .NET core?

ConfigurationManager was added to support ASP.NET Core's new WebApplication model, used for simplifying the ASP.NET Core startup code.

What is the use of ConfigurationManager Appsettings?

Retrieves a specified configuration section for the current application's default configuration.

What is ConfigurationManager C#?

ConfigurationManager is the class which helps to read data from configurations. Provides access to configuration files for client applications. Namespace: System.Configuration. To use the ConfigurationManager class, your project must reference the System.


1 Answers

WebConfigurationManger knows how to deal with configuration inheritance within a web application. As you know, there could be several web.config files in one applicaion - one in the root of the site and any number in subdirectories. You can pass path to the GetSection() method to get possible overridden config.

If we'd looke at WebConfigurationManager with Reflector then things are clear:

public static object GetSection(string sectionName) {     ...     return ConfigurationManager.GetSection(sectionName); }  public static object GetSection(string sectionName, string path) {     ...     return HttpConfigurationSystem.GetSection(sectionName, path); } 
like image 180
XOR Avatar answered Oct 13 '22 19:10

XOR