C# WebConfigurationManager 和 ConfigurationManager 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/698157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between the WebConfigurationManager and the ConfigurationManager?
提问by John Bubriski
What's the difference between the WebConfigurationManager
and the ConfigurationManager
?
有什么之间的区别WebConfigurationManager
和ConfigurationManager
?
When should I use one over the other?
我什么时候应该使用一个?
UPDATED
更新
I just looked at the WebConfigurationManager
, and for some reason, you can't access the connection strings as you do in the ConfigurationManager
(like an array). Can anyone tell me why MS made it like this? It seems to be a pain to get the connection string you need using the WebConfigurationManager
.
我只是查看了WebConfigurationManager
,出于某种原因,您无法ConfigurationManager
像在(如数组)中那样访问连接字符串。谁能告诉我为什么 MS 这样做?使用WebConfigurationManager
.
UPDATED AGAIN with CAVEAT!
CAVEAT 再次更新!
If you don't have a reference to the System.Configuration
namespace added to your project, then Visual Studio will show an error when you try and access the WebConfigurationManager.ConnectionStrings
like an array!
如果您没有对System.Configuration
添加到项目中的命名空间的引用,那么当您尝试访问WebConfigurationManager.ConnectionStrings
数组时,Visual Studio 将显示错误!
采纳答案by XOR
WebConfigurationManger knows how to deal with configuration inheritance within a web application. As you know, there could be several web.config files in one applicaion - one in the root of the site and any number in subdirectories. You can pass path to the GetSection() method to get possible overridden config.
WebConfigurationManger 知道如何处理 Web 应用程序中的配置继承。如您所知,一个应用程序中可能有多个 web.config 文件 - 一个在站点的根目录中,而在子目录中则任意多个。您可以将路径传递给 GetSection() 方法以获得可能的覆盖配置。
If we'd looke at WebConfigurationManager with Reflector then things are clear:
如果我们使用 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);
}
回答by Konstantin Tarkus
WebConfigurationManager is made specifically for ASP.NET applications.
WebConfigurationManager 是专门为 ASP.NET 应用程序制作的。
WebConfigurationManager provides additional methods to load configuration files applicable to Web applications.
WebConfigurationManager 提供了额外的方法来加载适用于 Web 应用程序的配置文件。
ConfigurationManager provides also methods to load configuration files applicable to ".exe" applications.
ConfigurationManager 还提供了加载适用于“.exe”应用程序的配置文件的方法。
I'd suggest taking a look at WebConfigurationManager and see if it provides you with anything you simply cannot do with ConfigurationManager and use it instead, otherwise using ConfigurationManager will make it far easier to have your code be used seamlessly between web and desktop aps.
我建议看一看 WebConfigurationManager,看看它是否为您提供了 ConfigurationManager 无法做的任何事情并使用它,否则使用 ConfigurationManager 将使您的代码在 Web 和桌面应用程序之间无缝使用变得更加容易。
回答by Zhaph - Ben Duguid
Not sure what you mean about the connection strings.
不确定您对连接字符串的意思。
Calling WebConfigurationManager.ConnectionStringsreturns a System.Configuration.ConnectionStringSettingsCollection, which is the same as you would get if you called ConfigurationManager.ConnectionStrings.
调用WebConfigurationManager.ConnectionStrings 会返回一个 System.Configuration.ConnectionStringSettingsCollection,这与调用ConfigurationManager.ConnectionStrings 时得到的结果相同。
Otherwise, as XOR says, it's designed to handle multiple hierarchical web.configs, combining them as required as you move around the folders in an application.
否则,正如 XOR 所说,它旨在处理多个分层 web.configs,当您在应用程序中的文件夹中移动时根据需要组合它们。
回答by WDuffy
Although WebConfigurationManager is located in the System.Web assembly the ConnectionStringSettingsCollection that it returns is located in System.Configuration.
尽管 WebConfigurationManager 位于 System.Web 程序集中,但它返回的 ConnectionStringSettingsCollection 位于 System.Configuration 中。
If you are getting the error
如果您收到错误
Cannot apply indexing with [] to an expression of type 'System.Configuration.ConnectionStringSettingsCollection'
无法将 [] 索引应用于“System.Configuration.ConnectionStringSettingsCollection”类型的表达式
while trying to access the array index...
在尝试访问数组索引时...
WebConfigurationManager.ConnectionStrings["Name"].ConnectionString
make sure you have a reference to assembly System.Configuration
确保您有对程序集 System.Configuration 的引用