差异行为之间的系统。网。配置。WebConfigurationManager和系统。配置。ConfigurationManager
-
20-09-2019 - |
题
我有一些麻烦,在测试的服务器ASP.NET 网站。我犯错了,并且有的家庭 目录的网站点指出了错误的地方。当我试图:
ConfigurationManager.ConnectionStrings["connectionString"];
它返回的空,但是
using System.Web.Configuration;
/* ... */
var rootWebConfig =
WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
rootWebConfig.ConnectionStrings.ConnectionStrings["connectionString"].ConnectionString;`
返回正确的连接串。
什么之间的所有差异的两个方法?
编辑:我真的要问的是,为什么 ConfigurationManager
方法失败时的主目录是不正确的设置,但以成功,否则,和 WebConfigurationManager
成功无论是否主目录是否正确?是否有任何其他的差异,例如假设有关访问控制?
解决方案
试试这个:
把一个断点你ConfigurationManager声明是与运行中的以下立即的输出窗口
((ConfigurationSection) ConfigurationManager.GetSection("connectionStrings")).ElementInformation
.我的机器报告 资料来源: "C:\Users\John\Documents\Visual Studio2008\项目\StackOverflowCode\WebApplication1\网。config" 如下。
注:下面也显示了我的访问ASP.NET 网。config。
{System.Configuration.ElementInformation}
Errors: {System.Configuration.ConfigurationException[0]}
IsCollection: true
IsLocked: false
IsPresent: true
LineNumber: 17
Properties: {System.Configuration.PropertyInformationCollection}
Source: "C:\\Users\\John\\Documents\\Visual Studio 2008\\Projects\\StackOverflowCode\\WebApplication1\\web.config"
Type: {Name = "ConnectionStringsSection" FullName = "System.Configuration.ConnectionStringsSection"}
Validator: {System.Configuration.DefaultValidator}
当我运行 ConfigurationManager.ConnectionStrings.ElementInformation
我得到 来源:null 这是正确的我的网页应用程序。
你怎么得到的配置来源的路径的???
立即假设
ConfigurationManager.ConnectionStrings["connectionString"];
看起来可能用于配置位置并不一定是相同的网络应用程序的根本网络。config。有可能是在寻找一个窗口目录(e。g在一个不同的地方或机器。config).试图找到一个适当的测试这个。
意图的每个经理
系统。配置。ConfigurationManager 可以访问。网络配置的XML格式,这意味着它会读取:
- 网络配置(即网。配置文件ASP.NET)
- 并非网络配置(例如应用程序。配置文件的独立控制台应用程序,Windows的应用程序,等等。)
并表示这些方面是共同类型的配置。这是一个通用的配置管理。 (然而尽管有这种能力来看待这两种类型的配置,应该使用它的应用程序配置,因为该网管理专门网络的配置,如下所述...)
系统。网。配置。WebConfigurationManager 不会很多同样的事情,但是"webified"版本的配置管理,提供访问ASP.NET 网络的具体方面的结构(例如网。配置文件ASP.NET).
相似之处
请参阅成员之间的相似之处 ConfigurationManager.* 和 WebConfigurationManager.*
管理人员可以,例如,访问 AppSettings
酒店和一个 ConnectionStrings
财产。事实上这两个设置共有两种类型的配置和甚至位于同一水平在XML文件。
因此,有很多相似之处,但是,
行为差异
访问构成:ConfigurationManager有的方法来开独立的应用程序配置(即Myprogram.EXE's的应用程序。config)相对于执行程序,而WebConfigurationManager有方法打开的ASP.NET 网络配置相对于它的网络应用程序根目录的网站。
这是一个基本的 应用程序。config (例如打开过"C:\winapp\app.config"从一个磁盘上的文件夹 ConfigurationManager)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings/>
<connectionStrings/>
</configuration>
和这里的一个基本的 网。config (例如通过"~"的含义根本的网站 WebConfigurationManager)
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<!-- special web settings -->
</system.web>
</configuration>
通知的相似之处。还注意到该网络的配置具有一个附加的 system.web
元素ASP.NET.
这些管理人员都位于 不同的 大会。
- ConfigurationManager: System.Configuration.dll
- WebConfigurationManager: System.Web.dll
其他提示
首先类提供对一般客户端配置文件(如app.config)中和第二个用于web应用程序的文件(如web.config中)。