首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

ASP.NET 狀態管理 (10/13) - Profile 設定檔個人化

     編輯:關於ASP.NET

Profile 功能

將使用者唯一資訊以持久格式儲存,自動識別用戶、取得資料,無需自建資料表。

啟用與設定

<profile>
  <properties>
    <add name="PostalCode" />
    <add name="Theme" default="Blue" />
    <add name="ShoppingCart" type="MyApp.Cart" serializeAs="Binary" />
  </properties>
</profile>
  • serializeAs:String/Xml/Binary/Custom。
  • type:自訂型別完整名稱。
  • allowAnonymous:匿名用戶支援。

動態產生強型別類別

執行期 ASP.NET 產生繼承 ProfileBase 的 ProfileCommon 類別,屬性對應設定檔定義。HttpContext.Current.Profile 回傳此類別實例。

使用範例

// 寫入
Profile.PostalCode = txtPostalCode.Text;

// 讀取
var weather = GetWeatherInfo(Profile.PostalCode);

無需手動識別用戶、寫入資料庫,Provider 自動處理。

Provider 選擇

  • SqlProfileProvider:預設,SQL Server 儲存 (可指定連線字串)。
  • 自訂 Provider:繼承 ProfileProvider 實作 GetPropertyValues/SetPropertyValues。

總結

Profile 以聲明式設定 + 強型別存取 + Provider 抽象,大幅簡化使用者個人化資料管理,是會員制網站的標準選擇。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved