Asp.Net2.0中我们可以方便的访问配置文件中,.NetFrameWork2.0新增加了 SystemWebSectionGroup 类。
允许用户以编程方式访问配置文件的 system.web 组。
比如判断web.config内是否为 debug="true",或者判断身份验证形式
//get the web application configuration
//WebConfigurationManager.OpenWebConfiguration配置文件的虚拟目录,如果null则取根目录下的web.config
System.Configuration.Configuration configuration = WebConfigurationManager.OpenWebConfiguration("");
//get the <system.web> group
SystemWebSectionGroup systemWeb = (SystemWebSectionGroup)configuration.GetSectionGroup("system.web");
// Get the compilation section.
CompilationSection compilation =
systemWeb.Compilation;
// Read section information.
SectionInformation info =compilation.SectionInformation;
string name = info.SectionName;
string type = info.Type;
string declared = info.IsDeclared.ToString();
string msg = String.Format("Name: {0}/nDeclared: {1}/nType: {2}/n", name, declared, type);
Response.Write(msg);
AuthenticationSection auth = systemWeb.Authentication;
SectionInformation info = auth.SectionInformation;
string name = info.SectionName;
string type = info.Type;
string declared = info.IsDeclared.ToString();
Response.Write("name:"+name+"/n"+"Declared:"+declared+"/n"+"Type:"+type);
Response.Write(systemWeb.Debug);//获取Debug属性值
本示例只获取Authentication的配置内容,要获取其它的配置内容可以参见MSDN文档。
附:SystemWebSectionGroup 类的公共属性: (MSDN)
| 名称 | 说明 | |
|---|---|---|
| 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 节。 |
本文介绍了如何使用Asp.Net2.0中的SystemWebSectionGroup类来编程访问web.config配置文件中system.web部分的内容,如compilation、authentication等设置。示例代码展示了获取debug属性值和身份验证形式的方法。

被折叠的 条评论
为什么被折叠?



