|
使用 ConfigurationSettings 对象的 AppSettings 属性检索 ASP.NET 配置信息。
ASP.NET 允许开发人员通过直接公开配置设置(以强类型属性的形式)或使用常规配置 API,从应用程序中访问配置设置。下面的示例显示了一个使用 System.Web.HttpRequest 类的 Browser 属性访问 配置节的页。这是有关属性的哈希表,这些属性反映了当前正在访问页的浏览器客户端功能。实际的 节数据包含在 machine.config 文件中。
以下内容为程序代码:
<%@ Page Language="C#" %> 检索浏览器功能 Boolean ActiveXControls = <%=Request.Browser.ActiveXControls.ToString()%>
Boolean AOL = <%=Request.Browser.AOL.ToString()%>
Boolean BackgroundSounds = <%=Request.Browser.BackgroundSounds.ToString()%>
Boolean Beta = <%=Request.Browser.Beta.ToString()%>
String Browser = <%=Request.Browser.Browser%>
Boolean CDF = <%=Request.Browser.CDF.ToString()%>
Boolean Cookies = <%=Request.Browser.Cookies.ToString()%>
Boolean Crawler = <%=Request.Browser.Crawler.ToString()%>
Boolean Frames = <%=Request.Browser.Frames.ToString()%>
Boolean JavaApplets = <%=Request.Browser.JavaApplets.ToString()%>
Boolean JavaScript = <%=Request.Browser.JavaScript.ToString()%>
Int32 MajorVersion = <%=Request.Browser.MajorVersion.ToString()%>
Double MinorVersion = <%=Request.Browser.MinorVersion.ToString()%>
String Platform = <%=Request.Browser.Platform%>
Boolean Tables = <%=Request.Browser.Tables.ToString()%>
String Type = <%=Request.Browser.Type%>
Boolean VBScript = <%=Request.Browser.VBScript.ToString()%>
String Version = <%=Request.Browser.Version%>
Boolean Win16 = <%=Request.Browser.Win16.ToString()%>
Boolean Win32 = <%=Request.Browser.Win32.ToString()%>
|
除了如上所示访问配置设置外,开发人员还可使用 System.Configuration.ConfigurationSettings 类检索任意配置节的数据。注意,ConfigurationSettings 返回的具体对象取决于映射到配置节的节处理程序(请参阅 IConfigurationSectionHandler.Create)。 以下代码说明可以如何访问为 节公开的配置数据。在该示例中,假设配置节处理程序返回一个具有 Enabled 属性且类型为 CustomConfigSettings 的对象。 以下内容为程序代码:
CustomConfigSettings config = (CustomConfigSettings) ConfigurationSettings["customconfig"]; if (config.Enabled == true) { // Do something here. } Dim config As CustomConfigSettings = CType(ConfigurationSettings("customconfig"), CustomConfigSettings) If config.Enabled = True Then ' Do something here. End If var config:CustomConfigSettings = CustomConfigSettings(ConfigurationSettings["customconfig"]); if (config.Enabled == true) { // Do something here. } |
|