网站设计用于用户的反复访问。个性化允许站点记住用户身份和其他信息详细信息,并为每个用户提供个性化的环境。 ASP.NET提供用于个性化网站的服务,以适合特定客户的喜好和喜好。 了解配置文件ASP.NET个性化服务基于用户配置文件。用户配置文件定义有关站点所需用户的信息类型。例如,姓名,年龄,地址,出生日期和电话号码。 此信息在应用程序的web.config文件中定义,并且ASP.NET运行时读取并使用它。这项工作由个性化提供者完成。 从用户数据获得的用户配置文件存储在ASP.NET创建的默认数据库中。您可以创建自己的数据库来存储配置文件。概要文件数据定义存储在配置文件web.config中。 例让我们创建一个示例站点,我们希望我们的应用程序在该站点上记住用户详细信息,例如姓名,地址,出生日期等。在<system.web>元素内的web.config文件中添加配置文件详细信息。 <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> 当在web.config文件中定义了配置文件时,可以通过当前HttpContext中的Profile属性使用该配置文件,也可以通过页面使用该配置文件。 添加文本框以接受配置文件中定义的用户输入,并添加用于提交数据的按钮: 更新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; } } } } 为“提交”按钮编写以下处理程序,以将用户数据保存到配置文件中: 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(); } } 首次执行页面时,用户需要输入信息。但是,下次用户详细信息将自动加载。 <add>元素的属性除了我们使用的名称和类型属性之外,<add>元素还有其他属性。下表说明了其中一些属性:
匿名个性化匿名个性化允许用户在标识自己之前对站点进行个性化。例如,Amazon.com允许用户在登录之前在购物车中添加商品。要启用此功能,可以将web.config文件配置为: <anonymousIdentification enabled ="true" cookieName=".ASPXANONYMOUSUSER" cookieTimeout="120000" cookiePath="/" cookieRequiresSSL="false" cookieSlidingExpiration="true" cookieprotection="Encryption" coolieless="UseDeviceProfile"/> |