WebConfigurationManager和ConfigurationManager之间有什么区别?

Joh*_*ski 99 .net c# asp.net configurationmanager webconfigurationmanager

WebConfigurationManager和ConfigurationManager之间有什么区别?

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

更新

我只是查看了WebConfigurationManager,由于某种原因,您无法像在ConfigurationManager中那样访问连接字符串(如数组).谁能告诉我为什么MS这样做了?使用WebConfigurationManager获取所需的连接字符串似乎很痛苦.

再次更新CAVEAT!

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

XOR*_*XOR 92

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);
}
Run Code Online (Sandbox Code Playgroud)


Kon*_*kus 25

WebConfigurationManager专为ASP.NET应用程序而设计.

WebConfigurationManager提供了其他方法来加载适用于Web应用程序的配置文件.

ConfigurationManager还提供了加载适用于".exe"应用程序的配置文件的方法.

我建议看一下WebConfigurationManager,看看它是否为你提供了一些你无法用ConfigurationManager做的事情而是使用它,否则使用ConfigurationManager会让你的代码在web和桌面aps之间无缝地使用变得容易得多.