Asp.net用户个性化配置(Profile)的使用要点

来源:互联网 发布:更新驱动哪个软件知乎 编辑:程序博客网 时间:2024/06/16 04:16

用户个性化功能一直是博客、空间等社区性网站的一大功能。怎么说,这种类型的问题的吧在Asp.net下还真就有比较好的解决方案,做起来也不费力。

直接上步骤再说:

1.在Web.Config文件里启用用户配置。具体如下:

<profile defaultProvider="SqlProvider"><providers><add name="SqlProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ConnectionString"                    applicationName="/"/></providers><properties><add name="Columns" type="Northwind_Web.Common.ColumnsProfileCollection"/></properties></profile>

我们看到,配置节里有个Provider的节(这其实Asp.net中的Provider设计模式,有兴趣的可以了解一下)。这里我们用的是.Net自己提供的SqlProfileProvider,注意这里有个ConnectionStringName指向一个用来持久化保存个性化信息的数据库链接。所以下一步就要在数据库中配置出.Net提供的表。

2.在C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727目录(一般)下,有个aspnet_regsql.exe的工具(启用成员资格的工具,用户配置表也在其中,如果你做有用户权限的Asp.net网站不知道有这个东东,可以说你还是个Layman)。然后。。。

3.数据库配好之后,我们还要确定要持久化保存的个性化信息格式,也就是配置节里Properties里的内容。这里我用的是一个我自己定义的复杂数据格式,如果你只要保存一些简单的字符串信息也可以指定为String,反正都会反射出来。注意复杂数据格式像这里的ColumnsProfileCollection必需是可序列化的。

4.个性化配置工具类(代码偶合性,将配置操作完全封装)。一般工具类具有以下几项:

ProfileName  ----> StringProperties  -----> List<string>RestoreProfile(HttpContext context, ....)SaveColumnsProfile(HttpContext context,...)

一般做好这四步,那就只剩下调用的事了。但是还有些问题需要特殊处理。

A)匿名用户个性化配置迁移

网上有一篇用Asp.net Profile来做购物车应用的例子,里面就有用到这个匿名用户Profile迁移技术。一般我们在Web.Config中按以下配置

<anonymousIdentification enabled="true" />

就会启用匿名用户标识,这时它会在你本地存一个Cookie用来保存这个唯一标识号,这样就可以确保下次还会记录上一次的配置内容。然后再重写Global中的一个方法

        public void Profile_OnMigrateAnonymous(object sender, ProfileMigrateEventArgs args)        {            ProfileCommon profile = Context.Profile.GetProfile(args.AnonymousID);            //ProfileBase profile = ProfileBase.Create(args.AnonymousID);            //MVC 下用以上            ShoppingCart cart = profile["ShoppingCart"] as ShoppingCart;            if (cart.Count > 0)            {                this.Context.Profile["ShoppingCart"] = cart;                this.Context.Profile.Save();            }            Membership.DeleteUser(args.AnonymousID);            // Delete the anonymous profile. If the anonymous ID is not             // needed in the rest of the site, remove the anonymous cookie.            ProfileManager.DeleteProfile(args.AnonymousID);            AnonymousIdentificationModule.ClearAnonymousIdentifier();        }
其它就不赘述了,代码逻辑已经很明显。这里要注意的就是profile对象的获取,后面我们会解释为什么在MVC下会不一样。

B)Profile的管理操作

在上面的匿名用户Profile迁移中,我们会看到,后面使用了ProfileManager来对原来匿名的用户配置进行删除。这里就是一个个性化配置管理过程,对于无用的配置信息,或过期的配置应该尽早的删除以免加重数据库压力。置于ProfileManager的一些用法,这里就不赘述了,MSDN上一查一堆。

最后一个问题是关于在MVC下使用Profile的问题。

对于传统的Asp.net网站,系统根据Web.Config中的配置,自动帮开发者生成一个ProfileCommon的对象以方便进行Profile操作。但是在MVC下,你必须自己定义一个ProfileCommon来进行相应的操作。这也解释了MVC在匿名用户配置迁移中profile对象的获取,其实如果自定义了一个ProfileCommon类的话,也可以使用传统方式获取。

要让系统引用自已定义ProfileCommon类,要在Web.Config中设置profile配置节的inherits="CustomProfileCommon"。这里贴出一个CustomProfileCommon的例子

    public class ProfileCommon : ProfileBase    {        public virtual ColumnsProfileCollection Columns        {            get            {                return (ColumnsProfileCollection)this.GetPropertyValue("Columns");            }            set            {                this.SetPropertyValue("Columns", value);            }        }        public virtual ProfileCommon GetProfile(string username)        {            return ((ProfileCommon)(ProfileBase.Create(username)));        }    }

原创粉丝点击