关于web.config

调试模式提供一种特殊的编译模式,处于调试模式的网站会影响系统的性能.但是,有时候为了便于系统维护,我们会把一些调试时用到的功能模块集成到产 品网站中,为了不对产品的正常运行造成影响同时又能够在系统发生故障时方便的使用调试模块,我想到了好好利用下调试模式.

我们知道,调试模式配置是在web.config文件中的.

<compilation debug="true"> </compilation>

读取web.config文件需要用到.net 2.0新增加的一些针对配置文件的操作类.

这里用到的是System.Configuration.ConfigurationSettings类,它提供的 GetConfig(string name)方法能够定位到你要获取值的结点.

还有一个类,System.Web.Configuration.CompilationSection自然就是定义编译配置信息的类,这里面有一 个属性:Debug,它返回一个bool值,以指示当前是否启用了调试模式.

System.Web.Configuration.CompilationSection ds = (System.Web.Configuration.CompilationSection)System.Configuration.ConfigurationSettings.GetConfig("system.web/compilation"); isDebugEnable = ds.Debug;

上面代码中用到了一个类型转换,将获取到的结点类型转化为 System.Web.Configuration.CompilationSection类型,从而利用ds.Debug属性获得结果.

调试模式时我们可以集成一些有用的功能,这些功能有:

错误检测:检测系统输出的错误日志并在屏幕上显示

数据库查询检测:测试数据库的查询,并能更改数据库的部分信息,支持全部的查询语句并限制查询的权限

系统日志:在调试模式启动时,系统将记录所有的错误日志,而不是根据原先的优先级,便于查找Bug.

由于调试模式的启用必须要到服务器上去配置,因此,安全性上面有了一定的保障,可以确保只有管理员才能够启用.

===========================================================================================================

Asp.Net2.0中我们可以方便的访问配置文件中,.NetFrameWork2.0新增加了 SystemWebSectionGroup 类。
允许用户以编程方式访问配置文件的 system.web 组。
比如判断web.config内是否为 debug="true",或者判断身份验证形式

System.Configuration.Configuration configuration = WebConfigurationManager..OpenWebConfiguration("");)

//配置文件的虚拟目录,如果null则取根目录下的web.config


SystemWebSectionGroup ws = (SystemWebSectionGroup)configuration.GetSectionGroup("system.web");
CompilationSection cp = ws.Compilation;
用cp.Debug;就可以得到compilation节内关于"debug"的配置
AuthenticationSection as = ws.Authentication;
用 as.Mode 可以获取 authentication节中关于"mode"的配置,值为AuthenticationMode 枚举之一
AuthenticationMode 的取值如下:
成员名称 说明
Forms 将基于 ASP.NET 窗体的身份验证指定为身份验证模式。
None 不指定身份验证。
Passport 将 Microsoft Passport 指定为身份验证模式。
Windows 将 Windows 指定为身份验证模式。在使用 Internet 信息服务 (IIS) 身份验证方法(基本、简要、集成 Windows (NTLM/Kerberos) 或证书)时适用此模式。


附:SystemWebSectionGroup 类的公共属性:
名称 说明
AnonymousIdentification 获取 anonymousIdentification 节。
Authentication 获取 authentication 节。
Authorization 获取 authorization 节。
BrowserCaps 获取 browserCaps 节。
ClientTarget 获取 clientTarget 节。
Compilation 获取 compilation 节。
CustomErrors 获取 customErrors 节。
Deployment 获取 deployment 节。
DeviceFilters 获取 deviceFilters 节。
Globalization 获取 globalization 节。
HealthMonitoring 获取 healthMonitoring 节。
HostingEnvironment 获取 hostingEnvironment 节。
HttpCookies 获取 httpCookies 节。
HttpHandlers 获取 httpHandlers 节。
HttpModules 获取 httpModules 节。
HttpRuntime 获取 httpRuntime 节。
Identity 获取 identity 节。
IsDeclarationRequired 获取一个值,该值指示是否需要声明此 ConfigurationSectionGroup 对象。 (从 ConfigurationSectionGroup 继承。)
IsDeclared 获取一个值,该值指示是否已声明此 ConfigurationSectionGroup 对象。(从 ConfigurationSectionGroup 继承。)
MachineKey 获取 machineKey 节。
Membership 获取 membership 节。
MobileControls 获取 mobileControls 节。
Name 获取此 ConfigurationSectionGroup 对象的名称属性。(从 ConfigurationSectionGroup 继承。)
Pages 获取 pages 节。
ProcessModel 获取 processModel 节。
Profile 获取 profile 节。
Protocols 获取 protocols 节。
RoleManager 获取 roleManager 节。
SectionGroupName 获取与此 ConfigurationSectionGroup 关联的节组名称。(从 ConfigurationSectionGroup 继承。)
SectionGroups 获取一个包含所有 ConfigurationSectionGroup 对象的 ConfigurationSectionGroup 对象,这些对象是此 ConfigurationSectionGroup 对象的子对象。(从 ConfigurationSectionGroup 继承。)
Sections 获取一个 ConfigurationSectionCollection,它包含此 ConfigurationSectionGroup 中的所有 ConfigurationSection 对象。(从 ConfigurationSectionGroup 继承。)
SecurityPolicy 获取 securityPolicy 节。
SessionState 获取 sessionState 节。
SiteMap 获取 siteMap 节。
Trace 获取 trace 节。
Trust 获取 trust 节。
Type 获取或设置此 ConfigurationSectionGroup 对象的类型。(从 ConfigurationSectionGroup 继承。)
UrlMappings 获取 urlMappings 节。
WebControls 获取 webControls 节。
WebParts 获取 webParts 节。
WebServices 获取 webServices 节。
XhtmlConformance 获取 xhtmlConformance 节

原文地址:https://www.cnblogs.com/zhangji/p/2023203.html