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 类的公共属性: <script type="text/Javascript"> var ExpCollDivStr=ExpCollDivStr; ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl066893b77,"; var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl06img,"; </script> (MSDN)
名称 | 说明 | |
---|---|---|
AnonymousIdentification | 获取 anonymousIdentification 节。 | |
Authentication | 获取 authentication 节。 | |
Authorization | 获取 authorization 节。 | |
BrowserCaps |