System.Web.Configuration.WebConfigurationManager和System.Configuration.ConfigurationManager之间的行为差​​异

Dav*_*son 12 c# asp.net configurationmanager behavior webconfigurationmanager

我在使用ASP.NET网站的测试服务器上遇到了一些麻烦.我搞砸了,并且默认网站的主目录指向了错误的位置.当我尝试:

ConfigurationManager.ConnectionStrings["connectionString"]; 
Run Code Online (Sandbox Code Playgroud)

它返回null,但是

using System.Web.Configuration;

/* ... */

var rootWebConfig =
    WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

rootWebConfig.ConnectionStrings.ConnectionStrings["connectionString"].ConnectionString;` 
Run Code Online (Sandbox Code Playgroud)

返回正确的连接字符串.

这两种方法之间有什么区别?

编辑:我真正要问的是,为什么ConfigurationManager在主目录设置不正确时方法失败,否则成功,并且WebConfigurationManager无论主目录是否正确设置都成功?是否存在其他差异,例如关于访问控制的假设?

Joh*_*n K 31

试试这个:

在ConfigurationManager语句所在的位置放置一个断点,然后在"立即输出"窗口中运行以下命令 ((ConfigurationSection) ConfigurationManager.GetSection("connectionStrings")).ElementInformation .我的机器报告源: "C:\ Users\John\Documents\Visual Studio 2008\Projects\StackOverflowCode\WebApplication1\web.config",如下所示.

注意:以下还显示我正在访问ASP.NET web.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}
Run Code Online (Sandbox Code Playgroud)

当我运行时,ConfigurationManager.ConnectionStrings.ElementInformation我得到Source:null,这对我的网络应用程序是正确的.

你得到什么配置源路径???


立即假设

ConfigurationManager.ConnectionStrings["connectionString"];可能会查找配置位置,该位置不一定与Web应用程序的根web.config相同.可能它正在查找Windows目录(例如,在不同的地方或machine.config).尽管如此,试图找到一个合适的测试.


每位经理的意图

系统配置.ConfigurationManager可以访问.NET配置XML格式,这意味着它同时读取:

  • Web配置(即ASP.NET中的web.config文件)
  • 和非Web配置(例如app.config文件 - 独立控制台应用程序,Windows应用程序等)

并表达配置类型常见的那些方面.这是一个通用的配置管理器.(尽管有这种能力来查看这两种类型的配置,你应该将它用于应用程序配置,因为Web管理器专门用于Web配置,如下所述...)

System.Web.Configuration.WebConfigurationManager几乎完全相同,但它是配置管理器的"webified"版本,提供对ASP.NET Web特定配置方面的访问(例如ASP.NET中的web.config文件).

相似

请参阅ConfigurationManager.*WebConfigurationManager之间的成员相似性.*

例如,两位经理都可以访问AppSettings房产和ConnectionStrings房产.实际上,这两种配置对于这两种配置都是通用的,甚至位于XML文档中的同一级别.

所以有很多相似之处,

行为差异

访问配置:ConfigurationManager具有相对于EXEC应用程序打开独立应用程序配置(即Myprogram.EXE的App.config)的方法,而WebConfigurationManager具有相对于Web站点中的Web应用程序根目录打开ASP.NET Web配置的方法.

这是一个基本的app.config(例如,通过ConfigurationManager从磁盘文件夹中通过"C:\ winapp\app.config"打开)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings/>
  <connectionStrings/>
</configuration>
Run Code Online (Sandbox Code Playgroud)

这是一个基本的web.config(例如通过"〜"打开,意思是WebConfigurationManager的网站根目录)

<?xml version="1.0"?>
<configuration>  
    <appSettings/>
    <connectionStrings/>

    <system.web>
        <!-- special web settings -->
    </system.web>

</configuration>
Run Code Online (Sandbox Code Playgroud)

注意相似之处.另请注意,Web配置还有一个system.webASP.NET 的附加元素.

这些经理位于不同的集合中.

  • ConfigurationManager:System.Configuration.dll
  • WebConfigurationManager:System.Web.dll