正如前面介紹 Membership 和 Role 的順序,
這裡呢,我先介紹一下 Profile 的 Provider,
Profile 其預設的 Provider 是 AspNetSqlProfileProvider ,
不過您也可以在設定檔 web.config 中自訂 Provider ,
並且其中也還有很多屬性可以設定,
具體的請看註解(當然我列出的只是比較常用的屬性而已)
在這裡定義好了屬性後,便會自動產生兩個動態類 --- Profile 和 ProfileCommon
這兩個類呢是當您在 web.config 中定義了 profile 節後自動產生的,
並且是繼承自 ProfileBase 類,由於這兩個類是動態產生的,
而非 .NET Framework 和 自訂所有,所以在 MSDN 中您都是尋找不到這兩個類的資訊的,
定義好 web.config 後,
Profile 和 ProfileCommon 兩個類便可以在伺服器端代碼中使用了,
但是這裡,我遇到一個問題,
就是在 VS 2008 中的 Code-Behind 中無法使用這些屬性,
但是在頁面中嵌入伺服器代碼的方式卻是可以使用的
而在 VS 2005 中,兩種方式都可以使用,
(要看這個問題的具體描述可以看上上篇博文,但是現在還沒有得到解決辦法,
等問題解決後,一定及時公布)
由於存在上面的這個問題,所以我在完成有關 Profile 的 Demo 時,
都是使用將伺服器端代碼直接嵌入在 .aspx 頁面中,
而不是使用 Code-Behind 的這種方式來完成的,
不過這個並不會影響 Demo 的品質和內容,在這裡只是提醒一下,
當然咯,在 Code-Behind 裡面不能訪問的
只有上面的自動產生的 Profile 和 ProfileCommon 兩個類而已,
而像 ProfileBase , ProfileProvider , ProfileModule (這三個類很重要,以後會介紹)
這些類都是可以在 Code-Behind 中使用的,
下面就來看一個使用 Profile 的 Demo 吧,
首先,要使用 Profile ,必須先設定 web.config 檔案,
最簡單的設定方式是:(很多屬性都使用預設的就 OK 了)
定義好了 Profile 節後,
便可以直接在伺服器代碼中使用自動產生的 Profile 和 ProfileCommon 類了,
等下大家看後面的 Demo 的伺服器端代碼就一目瞭然了,
還有必須提醒一點的是,Profile 使用者檔案設定到底是用來做什麼的,
大家必須有一個徹底的瞭解,先從 Membership 講起吧,
在註冊為 Membership 使用者時,大家都知道,
只有使用者名稱,密碼,郵箱,密碼安全問題和解答這些最最基本的使用者的資訊,
但是對於一個網站的使用者來說,這些資訊是根本不夠的,比如,使用者的興趣愛好啊,
使用者擅長的技術啊,使用者的暱稱啊,使用者的個性簽名啊,
這些等等都必須通過另外的機制來實現,
這就是 Profile 的功效了,可以說 Profile 是 Membership 的一個補充,
還有的是用 Profile 來定義每個使用者的個性網頁,
比如使用者喜歡的背景色和前景色彩這些都可以成為使用者個人化檔案,
上面的就是解釋了 Profile 使用者個人化到底是個啥作用了,
接下來就要看一個 Demo 來示範如何在 web.config 中定義好屬性,
然後使用伺服器代碼訪問這些屬性,並且將使用者添加的個人化屬性儲存發到資料庫中,
Demo 是採用將 C# 代碼嵌入在 .aspx 中,
(前面已經解釋了我為什麼沒有使用 Code-Behind 的原因)
我的 web.config 呢就定義成和上面一樣,定義了住址,電話群,星座三個屬性,
<%@ Page Language="C#" AutoEventWireup="true"%>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!User.Identity.IsAuthenticated)
{
FormsAuthentication.RedirectToLoginPage();
}
}
protected void btnSavaProfile_Click(object sender, EventArgs e)
{
if (User == null)
{
lblMsg.Text = "您為不合法使用者!!!";
return;
}
if (!String.IsNullOrEmpty(txtAddress.Text))
{
Profile.住址 = txtAddress.Text;
}
if (!String.IsNullOrEmpty(txtPhone.Text))
{
if (rBtnListPhone.SelectedItem.Text == "家庭電話")
{
Profile.電話.家庭電話 = txtPhone.Text;
}
else
{
Profile.電話.行動電話 = txtPhone.Text;
}
}
if (!String.IsNullOrEmpty(txtConstellate.Text))
{
Profile.星座 = txtConstellate.Text;
}
Profile.Save();
lblMsg.Text = "恭喜您,儲存 Profile 成功!!!";
txtAddress.Text = "";
txtConstellate.Text = "";
txtPhone.Text = "";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 31%;
height: 205px;
}
.style2
{
height: 36px;
font-size: small;
}
.style3
{
height: 46px;
text-align: center;
}
.style4
{
height: 23px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div style="font-family: 微軟雅黑; font-size: small">
<table class="style1" style="border-color: #8000FF">
<tr>
<td>
住址
</td>
<td>
<asp:TextBox ID="txtAddress" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:RadioButtonList ID="rBtnListPhone"
runat="server" Height="49px" Width="112px">
<asp:ListItem Selected="True">家庭電話
</asp:ListItem>
<asp:ListItem>行動電話</asp:ListItem>
</asp:RadioButtonList>
</td>
<td>
<asp:TextBox ID="txtPhone" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
星座
</td>
<td class="style2">
<asp:TextBox ID="txtConstellate" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td class="style3" colspan="2">
<asp:Label ID="lblMsg" runat="server"
Style="color: #FF0066">
</asp:Label>
</td>
</tr>
<tr>
<td class="style4" style="text-align: right">
<asp:Button ID="btnSavaProfile" runat="server"
OnClick="btnSavaProfile_Click" Text="儲存 Profile" />
</td>
</tr>
</table>
<br />
<br />
</div>
</form>
</body>
</html>
上面的 Demo 還有一點需要提醒一下,
就是由於 Profile 可以說是 Membership 的一個補充,
所以,Profile 使用者個人化肯定也是針對使用者而言的,
所謂個人化就是每一個都可以根據自己的喜好進行設定,
所以每一個使用者都有自己的 Profile ,
既然如此,那肯定在儲存 Profile 個人化到資料庫時,
也肯定是根據使用者來進行儲存的
(在這裡我先不考慮 Profile 的匿名機制,事實上,
匿名機制也是產生了唯一的辨別使用者的 GUID 標識)
既然是根據使用者儲存的,那麼肯定也需要先登入一個使用者啊,
只有登入了使用者,取得了登入的使用者的使用者名稱,這樣才能夠進行儲存,
其 Profile.Sava()可以自動擷取當前的使用者名稱進行儲存 Profile .
還有就是上面的 Profile 這個靜態類呢,是動態產生的,其繼承自 ProfileBase 類。
下面就來看下效果吧,
因為要確定使用者,所以會先跳轉到登陸頁面進行登陸操作,
我以 XiaoZhen 登入吧
給 XiaoZhen 這個使用者儲存了 Profile 之後呢,
就來看資料庫 aspnet_profile 了
再看下面的就可以清楚的看到剛才上面儲存的 Profile 的結果了
在這裡,我再來解釋一下 Profile 在資料庫中儲存的格式,
其實呢,一開始,我想很多人都會想到應該是會在資料庫中擴充欄位,
也就是當使用者要加入住址時,便在資料庫中擴充一個欄位 Address ,
其實呢,微軟並不是這樣處理的,
不過,其實再想想也是可以明白上面那種擴充欄位的方式顯然是不合適的,
你想啊,我一添加一個屬性就添加一個欄位,那個資料庫的架構被搞的還行啊,
以後還談什麼架構,談什麼模式啊,全部都亂了,而且如果您一添加屬性就要添加欄位,
我想,這樣資料庫的效率也高不到那裡去,
微軟採用的呢,
其實是將 Profile 的屬性名稱儲存在 aspnet_profile 中的 PropertyNames 屬性中,
而其屬性相關的值則是儲存在 aspnet_profile 中的 PropertyValuesString 中,
並且在 PropertyNames 中還一併儲存了
每一個屬性在 PropertyValuesString 中的起始字元位置和長度。
比如上面的
就表示住址這個屬性的值在這一行對應得 PropertyValuesStrin 中的第 1 個字元起,
而長度是 4 ,字元數為 4,也就是對應的
這一篇博文呢,主要是講了一下 Profile 的 web.config 中的 profile 節,
同時還示範了 Profile 的最基本的用法及其在資料庫中的儲存方式等等的基本內容。
2010—2—09