System.Web.Configuration.WebConfigurationManager和System.Configuration.ConfigurationManager之间的行为差​​异 [英] Differences in behavior between System.Web.Configuration.WebConfigurationManager and System.Configuration.ConfigurationManager

查看:380
本文介绍了System.Web.Configuration.WebConfigurationManager和System.Configuration.ConfigurationManager之间的行为差​​异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET网站在测试服务器上的一些麻烦。我疯玩,并有家庭
默认Web站点的目录指向错误的地方。当我尝试:

I had some trouble on a test server with an ASP.NET website. I goofed, and had the home directory of the Default Web Site pointed to the wrong place. When I tried:

ConfigurationManager.ConnectionStrings["connectionString"]; 

它返回null,但

it returned null, but

using System.Web.Configuration;

/* ... */

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

WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

rootWebConfig.ConnectionStrings.ConnectionStrings["connectionString"].ConnectionString;` 

返回正确的连接字符串。

returned the correct connection string.

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

What are all the differences between the two approaches?

编辑:什么我真的问的是,当主目录设置错误为何 ConfigurationManager中办法失败了,但在其他方面成功,而 WebConfigurationManager 无论主目录是否正确设置成功?是否还有其他的差别,比如有关访问控制?

What I'm really asking is, why does the ConfigurationManager approach fail when the home directory is incorrectly set, but succeeds otherwise, and the WebConfigurationManager succeeds regardless of whether the home directory is correctly set? Are there any other differences, such as assumptions about access control?

推荐答案

把一个断点在您ConfigurationManager中语句,并运行在立即Output窗口中
    ((配置节)ConfigurationManager.GetSection(是connectionStrings))。ElementInformation
。我的机器报告 来源:C:\\用户\\约翰\\文档\\ Visual Studio 2008的\\项目\\计算器code \\ WebApplication1 \\ web.config文件的,如下图所示

Try This:

Put a breakpoint where your ConfigurationManager statement is and run the following in the Immediate Output window ((ConfigurationSection) ConfigurationManager.GetSection("connectionStrings")).ElementInformation . My machine reports Source: "C:\Users\John\Documents\Visual Studio 2008\Projects\StackOverflowCode\WebApplication1\web.config" as seen below.

注意:以下还显示该矿访问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}

当我运行 ConfigurationManager.ConnectionStrings.ElementInformation 我得到的来源:空的这是正确的我的网站应用程序。

And when I run ConfigurationManager.ConnectionStrings.ElementInformation I get Source:null which is correct for my web app.

那你得到的配置源路径???

What do you get for a configuration Source path???

ConfigurationManager.ConnectionStrings [的connectionString]; 可能会寻找一个配置文件位置是不一定相同的Web应用程序的根web.config。可能它看起来在Windows目录下(例如在不同的地方或machine.config中)。试图找到这虽然一个合适的测试。

ConfigurationManager.ConnectionStrings["connectionString"]; might look for a config location which isn't necessarily the same as the web application's root web.config. Likely it's looking in a Windows directory (e.g at a different place or for machine.config). Trying to find an appropriate test for this though.

<一个href=\"http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager%28VS.80%29.aspx\">System.Configuration.ConfigurationManager可以访问.NET配置XML格式,这意味着它同时读取:

System.Configuration.ConfigurationManager can access the .NET configuration XML format which means it reads both:


  • 网络配置(即在ASP.NET web.config文件)

  • 和非网络配置(例如app.config文件 - 独立的控制台应用程序,Windows应用程序等)

和前presses是常见的类型配置的那些方面。这是一个通用的配置管理器。的(然而,尽管这种能力来看待这两种类型的configs,你应该使用它的应用CONFIGS因为Web管理器是专门到网上的configs,如下所述...)

and expresses those aspects that are common to types of configuration. This is a general purpose config manager. (However despite this ability to look at both types of configs, you should use it for app configs because the web manager is devoted to the web configs, as described next ...)

<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.configuration.webconfigurationmanager%28VS.80%29.aspx\">System.Web.Configuration.WebConfigurationManager的确pretty同样的事情,但配置管理器的Web化版本,提供了访问配置的ASP.NET web相关的方面(在ASP.NET例如web.config文件)。

System.Web.Configuration.WebConfigurationManager does pretty much the same thing but is the "webified" version of configuration manager, providing access to ASP.NET web-specific aspects of configuration (e.g. web.config file in ASP.NET).

请参阅之间<成员的相似之处href=\"http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager_members%28VS.80%29.aspx\">ConfigurationManager.*和<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.configuration.webconfigurationmanager_members%28VS.80%29.aspx\">WebConfigurationManager.*

这两个管理者可以,例如,访问的AppSettings 属性和的ConnectionStrings 属性。的确这两个设置是共同的种配置的,并且甚至位于XML文档中的相同水平。

Both managers can, for example, access an AppSettings property and a ConnectionStrings property. Indeed both these settings are common to both kinds of config and are even located at the same level in the XML document.

所以有很多相似之处但是,

So there are many similarities however,

访问配置
ConfigurationManager中有一些方法来打开独立的应用程序CONFIGS(即Myprogram.EXE的App.config中)相对EXEC应用程序,而WebConfigurationManager有方法打开ASP.NET Web配置相对要它在网站上的Web应用程序根目录。

Accessing configuration: ConfigurationManager has methods to open standalone app configs (i.e. Myprogram.EXE's App.config) relative to the EXEC app, whereas WebConfigurationManager has methods to open the ASP.NET web config relative to it's web application root directory in the web site.

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

Here's a basic app.config (e.g. opened via "C:\winapp\app.config" from a disk folder by ConfigurationManager)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings/>
  <connectionStrings/>
</configuration>

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

And here's a basic web.config (e.g. opened via "~" meaning root of web site by WebConfigurationManager)

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

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

</configuration>

注意相似之处。还要注意的Web配置有ASP.NET额外的System.Web 元素。

这些经理位于不同的组件。


  • ConfigurationManager中: System.Configuration.dll

  • WebConfigurationManager: System.Web.dll中

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

这篇关于System.Web.Configuration.WebConfigurationManager和System.Configuration.ConfigurationManager之间的行为差​​异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆