小编典典

WebConfigurationManager和ConfigurationManager有什么区别?

c#

WebConfigurationManager和之间有什么区别ConfigurationManager

我什么时候应该使用另一个?

更新

我只是看着WebConfigurationManager,由于某种原因,您不能ConfigurationManager像在数组中那样访问连接字符串。谁能告诉我为什么MS会这样吗?使用来获取所需的连接字符串似乎很痛苦WebConfigurationManager

再次更新了CAVEAT!

如果您没有对System.Configuration添加到项目中的名称空间的引用,则当您尝试WebConfigurationManager.ConnectionStrings像数组一样访问Visual
Studio时,Visual Studio将显示错误!


阅读 496

收藏
2020-05-19

共1个答案

小编典典

WebConfigurationManger知道如何处理Web应用程序中的配置继承。如您所知,在一个应用程序中可能有多个web.config文件-
一个在站点的根目录中,而在子目录中则是任意数量。您可以将路径传递到GetSection()方法以获取可能的覆盖配置。

如果我们用Reflector看一下WebConfigurationManager,那么事情就很清楚了:

public static object GetSection(string sectionName)
{
    ...
    return ConfigurationManager.GetSection(sectionName);
}

public static object GetSection(string sectionName, string path)
{
    ...
    return HttpConfigurationSystem.GetSection(sectionName, path);
}
2020-05-19