ASP.NET 個(gè)性化

2022-06-27 15:45 更新

個(gè)性化

網(wǎng)站是為用戶的重復(fù)訪問而設(shè)計(jì)的。個(gè)性化允許一個(gè)網(wǎng)站記住用戶標(biāo)識和其他信息細(xì)節(jié),并且它給每個(gè)用戶提供了一個(gè)個(gè)人的環(huán)境。

ASP.NET 為滿足特性客戶的品味和喜好而個(gè)性化一個(gè)網(wǎng)站提供服務(wù)。

理解特征文件

ASP.NET 個(gè)性化服務(wù)基于用戶的特征文件。用戶特征文件定義了該網(wǎng)站需要用戶的信息。例如,名字,年齡,地址,出生日期和手機(jī)號碼。

這個(gè)信息被定義在應(yīng)用程序的 web.config 文件中并且 ASP.NET 運(yùn)行時(shí)間閱讀并使用它。這個(gè)工作由個(gè)性化提供者所完成。

用戶數(shù)據(jù)所含有的用戶特征文件被存儲在默認(rèn)的 ASP.NET 創(chuàng)建的數(shù)據(jù)庫中。你可以創(chuàng)建你自己的數(shù)據(jù)庫來存儲特征文件。特征文件數(shù)據(jù)定義被存儲在配置文件 web.config 中。

例子

讓我們創(chuàng)建一個(gè)樣本網(wǎng)站,那里我們想要我們的應(yīng)用程序記住用戶細(xì)節(jié),像名字,地址,出生日期等。在 web.config 文件中用 元素添加特征文件細(xì)節(jié)。

<configuration>
<system.web>

<profile>
   <properties>
      <add name="Name" type ="String"/>
      <add name="Birthday" type ="System.DateTime"/>

      <group name="Address">
         <add name="Street"/>
         <add name="City"/>
         <add name="State"/>
         <add name="Zipcode"/>
      </group>

   </properties>
</profile>

</system.web>
</configuration>

當(dāng)特征文件在 web.config 文件中被定義時(shí),特征文件可以通過在當(dāng)前的 HttpContext 中找到的 Profile 屬性使用并且通過頁面獲得。

添加 text box 來獲取在特征文件中定義的用戶輸入,添加一個(gè) button 來提交數(shù)據(jù):

1

更新 Page_load 來展示特征文件信息:

using System;
using System.Data;
using System.Configuration;

using System.Web;
using System.Web.Security;

using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
   protected void Page_Load(object sender, EventArgs e)
   {
      if (!this.IsPostBack)
      {
         ProfileCommon pc=this.Profile.GetProfile(Profile.UserName);

         if (pc != null)
         {
            this.txtname.Text = pc.Name;
            this.txtaddr.Text = pc.Address.Street;
            this.txtcity.Text = pc.Address.City;
            this.txtstate.Text = pc.Address.State;
            this.txtzip.Text = pc.Address.Zipcode;
            this.Calendar1.SelectedDate = pc.Birthday;
         }
      }
   }
}

為提交按鈕寫以下的句柄,將用戶數(shù)據(jù)存入特征文件中:

protected void btnsubmit_Click(object sender, EventArgs e)
{
   ProfileCommon pc=this.Profile.GetProfile(Profile.UserName);

   if (pc != null)
   {
      pc.Name = this.txtname.Text;
      pc.Address.Street = this.txtaddr.Text;
      pc.Address.City = this.txtcity.Text;
      pc.Address.State = this.txtstate.Text;
      pc.Address.Zipcode = this.txtzip.Text;
      pc.Birthday = this.Calendar1.SelectedDate;

      pc.Save();
   }
}

當(dāng)頁面第一次執(zhí)行時(shí),用戶需要輸入信息。但是,下一次用戶的細(xì)節(jié)將被自動加載。

元素的屬性

除了我們已經(jīng)使用過的名字和類型屬性,元素還有其它屬性。以下的表格展示了這些屬性中的一些:

屬性描述
name屬性的名字。
type類型默認(rèn)是 string 但是它允許任何完全的類名稱作為數(shù)據(jù)類型。
serializeAS當(dāng)序列化這個(gè)值時(shí)使用的格式。
readOnly只讀的特征文件值不能被改變,這個(gè)屬性默認(rèn)是 false。
defaultValue一個(gè)默認(rèn)的值,如果特征文件不存在或者沒有信息的話它被使用。
allowAnonymous一個(gè)指示這個(gè)屬性是否能和匿名文件使用的布爾值。
Provider應(yīng)該被用來管理這個(gè)屬性的特征文件提供者。

匿名個(gè)性化

匿名個(gè)性化允許用戶在標(biāo)識它們自己之前個(gè)性化網(wǎng)站。例如,Amazon.com 允許用戶在登錄前在購物車中添加物品。為了啟用此功能,web.config 文件可以被配置成以下:

<anonymousIdentification enabled ="true" cookieName=".ASPXANONYMOUSUSER"
   cookieTimeout="120000" cookiePath="/" cookieRequiresSSL="false"
   cookieSlidingExpiration="true" cookieprotection="Encryption"
   coolieless="UseDeviceProfile"/>
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號