|
源代码网推荐
ASP.NET 允许开发人员通过直接公开配置设置(以强类型属性的形式)或使用常规配置 API,从应用程序中访问配置设置。下面的示例显示了一个使用 system.Web.HttpRequest 类的 Browser 属性访问 配置节的页。这是有关属性的哈希表,这些属性反映了当前正在访问页的浏览器客户端功能。实际的 节数据包含在 machine.config 文件中。
以下内容为程序代码:
检索浏览器功能
Boolean ActiveXControls = Boolean AOL = Boolean BackgroundSounds = Boolean Beta = String Browser = Boolean CDF = Boolean Cookies = Boolean Crawler = Boolean Frames = Boolean JavaApplets = Boolean JavaScript = Int32 MajorVersion = Double MinorVersion = String Platform = Boolean Tables = String Type = Boolean VBScript = String Version = Boolean Win16 = Boolean Win32 =
除了如上所示访问配置设置外,开发人员还可使用 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. }
使用应用程序设置
配置文件非常适合存储自定义应用程序设置,如数据库连接字符串、文件路径或远程 XML Web 服务 URL。默认的配置节(在 machine.config 文件中定义)包括 节,可用于将这些设置存储为名称/值对的形式。下面的示例显示一个 配置节,该配置节定义应用程序的数据库连接字符串。
ConfigurationSettings 对象公开一个特殊的 AppSettings 属性,可用于检索这些设置:
以下内容为程序代码:
String dsn = ConfigurationSettings.AppSettings["pubs"]; Dim dsn As String = ConfigurationSettings.AppSettings("pubs") var dsn:String = ConfigurationSettings.AppSettings["pubs"];
以下内容为程序代码:
检索配置数据
BackColor="#ccccff" BorderColor="black" ShowFooter="false" CellPadding=3 CellSpacing="0" Font-Name="宋体" Font-Size="8pt" HeaderStyle-BackColor="#aaaadd" />
源代码网供稿. |