本次我們討論主要聚焦在以下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發信的技巧,將信件發送出去就可以了。
