c# - WebConfigurationManager 和 ConfigurationManager 有什么区别?

标签 c# .net asp.net configurationmanager webconfigurationmanager

WebConfigurationManager 之间有什么区别?和 ConfigurationManager

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

已更新

我刚刚查看了 WebConfigurationManager ,并且出于某种原因,您无法像在 ConfigurationManager 中那样访问连接字符串。 (就像一个数组)。谁能告诉我为什么 MS 会这样?使用 WebConfigurationManager 获取所需的连接字符串似乎很痛苦.

再次更新并警告!

如果您没有对 System.Configuration 的引用命名空间添加到您的项目,然后 Visual Studio 将在您尝试访问 WebConfigurationManager.ConnectionStrings 时显示错误就像一个数组!

最佳答案

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

关于c# - WebConfigurationManager 和 ConfigurationManager 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/698157/

相关文章:

c# - 在界面中创建通用列表属性

c# - 将不同的文件复制到输出目录以进行发布和调试?

C# 如何在 process.arguments 中使用目录空白?

c# - 拆分一个字符串,使其丢失 http ://and anything after the '.'

.net - TeamCity MSBuild Gallio NCover 报告

asp.net - Windows 事件日志权限

C# 调用 C++ dll 获取 EntryPointNotFoundException

c# - 将视频流传输到YouTube(API)

c# - 带参数登录 asp.net 重定向

javascript - 返回 PartialView 时在脚本内使用 TempData