什么是WebConfigurationManager和ConfigurationManager中的区别?区别、WebConfigurationManager、ConfigurationManager

2023-09-02 01:35:14 作者:策马西风

什么是WebConfigurationManager和ConfigurationManager中的区别?

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

当我应该用一个比其他?

When should I use one over the other?

更新时间:

我只是看着WebConfigurationManager,并由于某种原因,你无法访问连接字符串为您在ConfigurationManager做(如数组)。谁能告诉我为什么MS使得这样吗?这似乎是一个痛苦的得到你需要使用WebConfigurationManager连接字符串。

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.

与CAVEAT!再次修订

如果您还没有一个引用添加到您的项目中的System.Configuration命名空间,那么Visual Studio将显示一个错误,当您试图访问WebConfigurationManager.ConnectionStrings象阵!

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!

推荐答案

WebConfiguratonManger知道如何处理配置继承Web应用程序中。如你所知,有可能在一个applicaion几个的web.config文件 - 一个在站点的根目录和任何数量的子目录。你可以通过路径GetSection()方法来获取可能重写配置。

WebConfiguratonManger 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.

如果我们想looke在WebConfigurationManager与反射接下来的事情是清楚的:

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);
}