繁体   English   中英

WebConfigurationManager和ConfigurationManager之间有什么区别?

[英]What's the difference between the WebConfigurationManager and the ConfigurationManager?

WebConfigurationManagerConfigurationManager之间有什么区别?

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

更新

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

再次更新CAVEAT!

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

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

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

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

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

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

虽然WebConfigurationManager位于System.Web程序集中,但它返回的ConnectionStringSettingsCollection位于System.Configuration中。

如果您收到错误

无法将带有[]的索引应用于类型为“System.Configuration.ConnectionStringSettingsCollection”的表达式

在尝试访问数组索引时......

WebConfigurationManager.ConnectionStrings["Name"].ConnectionString

确保您具有对程序集System.Configuration的引用

不确定你对连接字符串的意思。

调用WebConfigurationManager.ConnectionStrings将返回System.Configuration.ConnectionStringSettingsCollection,这与您调用ConfigurationManager.ConnectionStrings时获得的相同。

否则,正如XOR所说,它旨在处理多个分层web.configs,当您在应用程序中移动文件夹时,根据需要组合它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM