当前位置:编程学习 > asp >>

ASP.NET配置

使用 ConfigurationSettings 对象的 AppSettings 属性检索 ASP.NET 配置信息。
    ASP.NET 允许开发人员通过直接公开配置设置(以强类型属性的形式)或使用常规配置 API,从应用程序中访问配置设置。下面的示例显示了一个使用 System.Web.HttpRequest 类的 Browser 属性访问 <browserCaps> 配置节的页。这是有关属性的哈希表,这些属性反映了当前正在访问页的浏览器客户端功能。实际的 <browserCaps> 节数据包含在 machine.config 文件中。

以下内容为程序代码:<%@ Page Language="C#" %>
<html>
 
<body style="font: 10.5pt 宋体">
  <h3>检索浏览器功能</h3>
  Boolean ActiveXControls = <%=Request.Browser.ActiveXControls.ToString()%><br>
  Boolean AOL = <%=Request.Browser.AOL.ToString()%><br>
  Boolean BackgroundSounds = <%=Request.Browser.BackgroundSounds.ToString()%><br>
  Boolean Beta = <%=Request.Browser.Beta.ToString()%><br>
  String Browser = <%=Request.Browser.Browser%><br>
  Boolean CDF = <%=Request.Browser.CDF.ToString()%><br>
  Boolean Cookies = <%=Request.Browser.Cookies.ToString()%><br>
  Boolean Crawler = <%=Request.Browser.Crawler.ToString()%><br>
  Boolean Frames = <%=Request.Browser.Frames.ToString()%><br>
  Boolean JavaApplets = <%=Request.Browser.JavaApplets.ToString()%><br>
  Boolean JavaScript = <%=Request.Browser.JavaScript.ToString()%><br>
  Int32 MajorVersion = <%=Request.Browser.MajorVersion.ToString()%><br>
  Double MinorVersion = <%=Request.Browser.MinorVersion.ToString()%><br>
  String Platform = <%=Request.Browser.Platform%><br>
  Boolean Tables = <%=Request.Browser.Tables.ToString()%><br>
  String Type = <%=Request.Browser.Type%><br>
  Boolean VBScript = <%=Request.Browser.VBScript.ToString()%><br>
  String Version = <%=Request.Browser.Version%><br>
  Boolean Win16 = <%=Request.Browser.Win16.ToString()%><br>
  Boolean Win32 = <%=Request.Browser.Win32.ToString()%><br>
</body>
</html>
 

    除了如上所示访问配置设置外,开发人员还可使用 System.Configuration.ConfigurationSettings 类检索任意配置节的数据。注意,ConfigurationSettings 返回的具体对象取决于映射到配置节的节处理程序(请参阅 IConfigurationSectionHandler.Create)。 以下代码说明可以如何访问为 <customconfig> 节公开的配置数据。在该示例中,假设配置节处理程序返回一个具有 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 文件中定义)包括 <appSettings> 节,可用于将这些设置存储为名称/值对的形式。下面的示例显示一个 <appSettings> 配置节,该配置节定义应用程序的数据库连接字符串。
<configuration>
  <appSettings>
    <add key="pubs" value="server=(local)NetSDK;database=pubs;Trusted_Connection=yes" />
    <add key="northwind" value="server=(local)NetSDK;database=northwind;Trusted_Connection=yes" />
  </appSettings>
</configuration>
ConfigurationSettings 对象公开一个特殊的 AppSettings 属性,可用于检索这些设置: 
以下内容为程序代码:String dsn = ConfigurationSettings.AppSettings["pubs"];
Dim dsn As String = ConfigurationSettings.AppSettings("pubs")
var dsn:String = ConfigurationSettings.AppSettings["pubs"];
 
 
以下内容为程序代码:<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Configuration" %>
<html>
 
<script language="C#" runat="server">
    void Page_Load(Object Src, EventArgs E ) {
        String dsn = ConfigurationSettings.AppSettings["pubs"];
        SqlConnection myConnection = new SqlConnection(dsn);
        SqlDataAdapter myCommand = new SqlDataAdapter("select * from Authors", myConnection);
        DataSet ds = new DataSet();
        myCommand.Fill(ds, "作者");
        MyDataGrid.DataSource=new DataView(ds.Tables[0]);
        MyDataGrid.DataBind();
    }
</script>
<body>
  <h3><font face="宋体">检索配置数据</font></h3>
  <ASP:DataGrid id="MyDataGrid" runat="server"
    BackColor="#ccccff"
    BorderColor="black"
    ShowFooter="false"
    CellPadding=3
    CellSpacing="0"
    Font-Name="宋体"
    Font-Size="8pt"
    HeaderStyle-BackColor="#aaaadd"
  />
</body>
</html>

补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,