ASP.NET Web.config文件的配置,Configuration API

本次我们讨论主要聚焦在以下Web.config配置文件的设置值的读取。

1、<connectionString />连接字符串的读取。

2、<appSettings />应用程序设置值的读取。

3、<mailSettings />SMTP Mail设置值的读取。

在讨论读取上面3个节点配置钱我们先讨论一下下面3个类:

1、Configuration类(System.Configuration.Configuration)。

2、WebConfigurationManager类(System.Web.Configuration.WebConfigurationManager)。

3、ConfigurationManager类(System.Configuration.ConfigurationManager)。

以下大致介绍这3个类的功能。

1、Configuration类:

可将Configuration类视为.NET应用程序的配置本体(包含Web或Windows两类应用程序),通过它访问ASP.NET网站的Web.config文件或Windows Form专案的app.config文件。但Configuration类必须要和WebConfigurationManager或ConfigurationManager类搭配,要看应用程序是ASP.NET或Windows Form类型,ASP.NET使用WebConfigurationManager类,Windows Form使用ConfigurationManager类。

2、WebConfigurationManager类:

WebConfigurationManager类是提供对Web.config的访问,例如,以WebConfigurationManager类开启Web.config某个部分(Section),再返回交由Configuration类来进行处理。

3、ConfigurationManager类

ConfigurationManager类是提供对app.config的访问权,例如,以ConfigurationManager类开启app.config某个部分,再返回交由Configuration类来进行处理。

总结归纳:最终的搭配使用方式。

1)、ASP.NET网页:Configuration类+WebConfigurationManager类。

2)、Windows Forms类型:Configuration类+ConfigurationManager类。

附注:

A、虽WebConfigurationManager与ConfigurationManager类功能是互通的,但微软建设还是清楚区分使用。

B、WebConfigurationManager与ConfigurationManager都属于静态类,可直接取用,不需要new一个instance实例。

我们这次并非对以上3大类的功能进行详细的讲解,而是聚焦在以下Web.config配置文件的设置值的读取。

<connectionStrings />、<appSettings />、<mailSettings />的读取。

我们就是用以上讲解的3大类读取Web.config文件中的配置文件部分。

以下是一个简单的项目Web.config配置:

<?xml version="1.0" encoding="utf-8"?>

<!--

有关如何配置 ASP.NET 应用程序的详细消息,请访问

http://go.microsoft.com/fwlink/?LinkId=169433

-->

<configuration>

<appSettings>

<add key="name" value="ASP.NET"/>

<add key="desc" value="IT学习分享"/>

</appSettings>

<!--数据库连接设置-->

<connectionStrings>

<add name="FlightData" connectionString="Data Source=.;Initial Catalog=FlightData;Integrated Security=True" providerName="System.Data.SqlClient"/>

</connectionStrings>

<!--SMTP设置-->

<system.net>

<mailSettings>

<smtp from="yongguang1126@sina.com">

<network host="192.168.0.132" password="" userName=""/>

</smtp>

</mailSettings>

</system.net>

<system.web>

<compilation debug="true" targetFramework="4.0" />

</system.web>

</configuration>

1、读取<connectionStrings>部分连接字符串设置值

在此以WebConfigurationManager类的ConnectionStrings属性读取Web.config文件中<connectionStrings>部分的连接字符串设置值:

第一步:首先引用命名空间:System.Web.Configuration

第二步:引用了命名空间,我们就可以读取了

string conStr=WebConfigurationManager.ConnectionStrings["FlightData"].ConnectionString;

Response.Write("FlightData连接字符串:"+conStr);

2、读取<appSettings>部分应用程序设置值

在此以WebConfigurationManager类的AppSettings属性读取Web.config文件中<appSettings>部分应用程序设置值:

string name=WebConfigurationManager.AppSettings["name"];

Response.Write("名称:"+name+"<br />");

string desc=WebConfigurationManager.AppSettings["desc"];

Response.Write("描述:"+desc);

说明:

读取应用程序设置与读取连接字符串差不多,只差在关键词上。此外,Key(关键词)使用中文命名若发生读不到值的情况,改用英文即可。

3、读取<mailSettings>部分SMTP Mail设置

以前的ASP.NET版本对于SMTP Mail主机设置不是在程序中设置固定,就是在Web.config的ConfigurationSetting.AppSettings中指定,然后读取设置值。到了ASP.NET4.0,如果还沿用以前的做法,表面上看来好像也能读到SMTP设置,但会出现一些问题,如ASP.NET的Password Recovery控件是自动抓取SMTP设置,以发送新密码信函,故改用新的方式才不会出错,如下:

//引用命名空间

using System.Net.Configuration;

using System.Web.Configuration;

//读取实例

//开启Request所在路径网站的Web.config文件

Configuration config=WebConfigurationManager.OpenWebConfiguration(this.Request.ApplicationPath);

//取得Web.config中Mail设置部分

MailSettingsSectionGroup netSmtpMailSection=(MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");

//读取显示mailSettings设置相关值

Response.Write("Mail主机名:"+netSmtpMailSection.Smtp.Network.Host+"<br />");

Response.Write("Mail主机Port:"+netSmtpMailSection.Smtp.Network.Port+"<br />");

Response.Write("Mail消息:"+netSmtpMailSection.Smtp.From+"<br />");

//如果Mail的Authentication验证模式选择Basic,则可读取UserName及Password

//Response.Write("用户姓名:"+netSmtpMailSection.Smtp.Network.UserName+"<br />");

//Response.Write("用户密码:"+netSmtpMailSection.Smtp.Network.Password+"<br />");

说明:

程序读取到SMTP设置值后,再引用到ASP.NET发信的技巧,将信件发送出去就可以了。