您的位置:首页 > 编程语言 > ASP

如何处理ASP.NET 2.0配置文件

2007-07-30 10:13 886 查看
        第二版ASP.NET包含许多处理配置文件方面的改进。配置文件涉及一系列ASP.NET设置,同时方便了定制数据元素的使用。
        虽然使用ASP.NET 1.1恢复数据值并非难事,但2.0中包含的改进使这一操作更加方便,并且增加了更多特性。下面我将说明如何访问存储在web.config文件中的数据值。

新方法

 ASP.NET 2.0推出大量改进,改善了在配置文件中存储和恢复数据元素的方式。这些改进包含在web.config文件的几个新配置区域中,同时为加密和解密存储在配置文件中的数据值提供一个简便的方法。

 能够以编程的方式开发和管理配置数据,从而有时不必手工编辑隐藏XML,是ASP.NET 2.0的一项主要改进。配置API提供这项功能。它还包括一组与配置文件交互的类。

基本类

 配置API的基本类为Configuration类。这个类呈现应用于一个特殊物理视图(如一台计算机)或一个逻辑实体(如一个应用程序或网站)的配置设置的合并视图。如果没有配置文件,则Configuration类使用machine.config文件中定义的默认设置。

 Configuration类有两个方法,可按区域名访问配置文件中的数据:GetSection和GetSectionGroup。(MSDN提供一个区域名列表。)

GetSection:按名称恢复一个配置区域。它返回指定的ConfigurationSection对象。

GetSectionGroup:为指定的路径恢复一个ConfigurationSectionGroup对象。

 这两个方法允许你用XPath给配置文件中的区域指定路径。例如,下面的XPath表达式返回一个web.config文件的验证区域。

system.web/authentication

 运行ASP.NET应用程序时则使用WebConfigurationManager类。它包括开放式的方法,返回Configuration类的一个实例,Configuration类提供处理文件的方法和属性。

WebConfigurationManager类

 应用Web应用程序时,WebConfigurationManager类提供访问配置文件的功能。这个类包含在System.Web.Configuration命名空间中。这个类中包括许多与可能出现在配置文件中的预先定义的区域相对应的类。列表A中是一个ASP.NET项目的基本web.config文件。

<?xml version="1.0"?>

<configuration>

<appSettings>

<add key="site" value="TechRepublic.com"/>

</appSettings>

<connectionStrings>

<add name="db" connectionString="connection details"/>

</connectionStrings>

<system.web>

<compilation debug="false" />

<authentication mode="Windows" />

<authorization>

<allow users="tester"/>

</authorization>

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">

<error statusCode="403" redirect="NoAccess.htm" />

<error statusCode="404" redirect="FileNotFound.htm" />

</customErrors>

</system.web>

</configuration>

 该文件包含以下标准区域:appSettings、connectionStrings、compilation、authentication和custiomErrors。WebConfigurationManager类中含有将这些区域作为对象处理的方法。下表是这个列表的一个子集。你可以在System.Web.Configuration命名空间找到一个更加详细的综合列表。
CustiomErrorsSection:访问web.config中定义错误处理的Custom Errors区域。

AuthenticationSection:允许你定义应用程序如何处理用户验证。

AuthorizationSection:允许和拒绝用户或群体访问。

列表B中的C#代码访问这些区域并显示每个区域的值。

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Web.Configuration" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Load(object sender, EventArgs e) {

AuthenticationSection asec;

AuthorizationSection auth;

CustomErrorsSection cerr;

asec = (AuthenticationSection) WebConfigurationManager.GetSection("system.web/authentication");

auth = (AuthorizationSection) WebConfigurationManager.GetSection("system.web/authorization");

cerr = WebConfigurationManager.GetSection("system.web/customErrors") as CustomErrorsSection;

if (asec != null) {

Response.Write("<br>Authentication Mode: " + asec.Mode.ToString());

}

if (auth != null) {

for (int i = 0; i < (auth.Rules.Count - 1); i++) {

Response.Write("<br>Customer Errors mode: " + auth.Rules[i].Action.ToString() + " - " + auth.Rules[i].Users.ToString());

} }

if (cerr != null) {

Response.Write("<br>Customer Errors mode: " + cerr.Mode.ToString());

} }

</script>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>Configuration Class</title>

</head><body></body></html> 

 你还可以访问ASP.NET 2.0中新增的connectionString区域和appSettings区域,后者允许你存储一个定制数据元素。列表C中的ASP.NET页面(用C#编写)访问这些区域,并使用几种途径。它用它的类(connectionStringSection)或直接使用配置文件列表中连接字符串的索引值访问connectionString。

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Web.Configuration" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Load(object sender, EventArgs e) {

ConnectionStringsSection connectionStringsSection = WebConfigurationManager.GetSection("connectionStrings") as ConnectionStringsSection;

string connString = WebConfigurationManager.ConnectionStrings["db"].ToString();

string dvalue = WebConfigurationManager.AppSettings["site"].ToString();

Response.Write("<br />Connection String: " + connectionStringsSection.ConnectionStrings[1].ToString());

Response.Write("<br />Connection String: " + connString);

Response.Write("<br />Site value: " + dvalue);

}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Configuration Class</title>

</head><body></body></html> 

 你还能够以集合的方式访问区域和它们的值。列表D中的C#代码片段说明如何遍历connectioString区域中的所有值。

ConnectionStringsSection cStringsSection = WebConfigurationManager.GetSection("connectionStrings") as ConnectionStringsSection;

ConnectionStringSettingsCollection cStrings = cStringsSection.ConnectionStrings;

IEnumerator cStringsEnum = cStrings.GetEnumerator();

int j = 0;

while (cStringsEnum.MoveNext()) {

string name = cStrings[j].Name;

Response.Write("<br>Name: " + name + " Value: " + cStrings[name]);

j += 1;



结论

 ASP.NET web.config文件使得开发者更方便在应用程序中保存应用程序配置。ASP.NET 2.0推出的改进简化了配置数据的恢复、存储和加密过程。虽然本文主要针对ASP.NET Web应用程序,但配置API提出的所有改进都可适用于Windows窗体应用程序(Windows form application)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息