WebConfigurationManager和ConfigurationManager有什么不同
什么时候我应该使用一个而不是另一个?
已更新
我刚刚查看了WebConfigurationManager,由于某些原因,您无法像在ConfigurationManager中那样访问连接字符串(如数组)。谁能告诉我为什么微软会变成这样?使用WebConfigurationManager获取所需的连接字符串似乎很麻烦。
再次更新,但有警告!
如果您没有将对System.Configuration名称空间的引用添加到项目中,那么当您尝试像访问数组一样访问WebConfigurationManager.ConnectionStrings时,Visual Studio将显示一个错误!
发布于 2009-03-30 17:50:35
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);
}发布于 2009-03-30 17:22:48
WebConfigurationManager是专门为ASP.NET应用程序设计的。
WebConfigurationManager提供了加载适用于Web应用程序的配置文件的其他方法。
ConfigurationManager还提供了加载适用于".exe“应用程序的配置文件的方法。
我建议看看WebConfigurationManager,看看它是否为你提供了ConfigurationManager不能做的事情,然后使用它,否则使用ConfigurationManager会让你的代码更容易在web和桌面aps之间无缝使用。
发布于 2015-01-31 19:28:30
尽管WebConfigurationManager位于System.Web程序集中,但它返回的ConnectionStringSettingsCollection位于System.Configuration中。
如果您收到错误消息
对于'System.Configuration.ConnectionStringSettingsCollection‘类型的表达式,
不能应用带[]的索引
尝试访问数组索引时...
WebConfigurationManager.ConnectionStrings["Name"].ConnectionString确保您具有对程序集System.Configuration的引用
https://stackoverflow.com/questions/698157
复制相似问题