该文件包含以下标准区域: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.Action.ToString() " - " auth.Rules.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)。
Tony Patton拥有丰富的Java、VB、Lotus及XML认证方面的知识,是一个专业的应用程序开发人员。