2006年12月22日所有随笔

Web Application Project 模式下的 ProfileCommon 类的问题

  假如我们在 ASP.NET 程序中使用了用户配置文件(Profile),也就是应用程序配置文件(web.config)中包含了配置文件的属性(<profile>节)的定义,那么 ASP.NET 会自动生成一个名为 ProfileCommon 的类,继承于 System.Web.Profile.ProfileBase 类,MSDN 有如下说明:

    ASP.NET 使用 ProfileBase 类创建用于用户配置文件的类。在启动启用了用户配置文件的应用程序时,ASP.NET 会创建一个类型为 ProfileCommon 的新类,该类从 ProfileBase 类继承。强类型访问器被添加到 profile 配置节中为每个属性定义的 ProfileCommon 类中。

  我们在类视图可以看到 ProfileCommon 类的定义,打开一看,看路径,它不放在我们自己程序的目录下,而是放在 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files 目录下的某个目录(和你的项目名称同名)里,Temporary ASP.NET Files 目录是 asp.net 在编译和调试时的临时目录,一般是放那些 ASP.NET 自动生成的代码或 DLL 文件,像上面所说的 ProfileCommon 类文件就放在里面。

  当程序运行时,ProfileCommon 类的一个实例被设置为 ASP.NET 应用程序的 Profile 属性的值。也就是 HttpContext.Profile 属性的值,并为每一个 .aspx 页和 .ascx 页添加了一个 Profile 属性,我们在 Temporary ASP.NET Files 目录下就可在相应页的 .cs 文件找到这些代码:

    // WebUserControl.ascx
    public partial class WebUserControl {
        protected ProfileCommon Profile {
            get {
                return ((ProfileCommon)(this.Context.Profile));
            }
        }

        protected System.Web.HttpApplication ApplicationInstance {
            get {
                return ((System.Web.HttpApplication)(this.Context.ApplicationInstance));
            }
        }
    }

  然后,我们就可以在页上用 Profile 属性来访问用户的配置文件属性的值了:

    protected void Page_Load(object sender, EventArgs e)
    {
        Profile.CartInfo = "Good";

        string user = Profile.UserInfo;
    }

    要注意的是,以上所说的情况只适应于 Web Site 模式,在 Web Application Project 模式里是不支持的。


    Web Application Project 模式也自动生成 ProfileCommon 类(可从 Temporary ASP.NET Files 目录下找到),但是以 Web Site 模式生成,在类视图也不到,也没自动为每一个 .aspx 页和 .ascx 页添加了 Profile 属性,这么一来显示是没法通过页的 Profile 属性去访问配置文件的属性值,通过手工方式把 Temporary ASP.NET Files 目录里的 ProfileCommon 类的代码加到自己的程序里,也同样无法解决这个问题。Visual Studio 2005 SP1 在12月14日已经发布,SP1 已包经含了 Web Application Project!但不知有没把这个特性加上去(今天弄了一天还没把 SP1 装,也有人说 VS 2005 SP1 是 MS 最差劲的补丁程序,安装时间比原版的要长,空间占的多!)。

    还是说说上面所说的问题吧。ASP.NET 没自动生成就得靠自己手工生成了,可以到:

    http://www.gotdotnet.com/workspaces/workspace.aspx?id=406eefba-2dd9-4d80-a48c-b4f135df4127

    下载一个 ASP.Net WebProfile Generator,它是个 VS IDE 外接程序,它会根据你的 Web.config 文件里的<profile>配置节的配置,自动生成类似于 ProfileCommon 类的一个类 WebProfile(具体如何操作看里面的 readme.txt)。生成后最好不要手工去改动这个类,当你的配置文件有改动<profile>节时,只要重新生成一次就行了,更不要把类名改为 ProfileCommon,因为 ASP.Net 一见到有 ProfileCommon 就高兴,就会自动给每个 .aspx 页和 .ascx 页添加一个 Profile 属性,但又没去理会 WebProfile Generator 的意思,最终导致运行时还是有错误。

    自动生成 WebProfile 后,还要在需要读取到配置文件属性的页添加一个属性,以便操作用户配置文件属性:

    public partial class WishList : System.Web.UI.Page
    {
        // 手动添加此属性 **************
        private WebProfile Profile
        {
            get { return new WebProfile(Context.Profile); }
        }
        // **************

        protected void Page_Load(object sender, EventArgs e)
        {
            Profile.UserInfo = "pcvc.net";
        }
    }

    也可以这样:

    string s = WebProfile.Current.MyProperty;
    WebProfile.Current.MyGroup.MyProperty = "value";


    注意,ASP.Net WebProfile Generator 只能用于 Visual Studio 2005 英文版的 Web Application Project 模式下。

Visual Studio 2005 Service Pack 1 (SP1) Released

Visual Studio 2005 SP1 已于2006年12月14日发布了!可到以下地址下载:

http://msdn.microsoft.com/vstudio/support/vs2005sp1/default.aspx

安装程序有 400 多M,要注意的是在 windows xp 或 windows 2003 安装可能会发生:

安装大 WindowsInstaller 包或大型 WindowsInstaller 修补程序包时错误信息: " Error 1718。 文件被数字签名策略拒绝 "


要解决此问题, 请按照下列步骤:

1. 打开 控制面板 的 管理工具。
2. 双击 本地安全策略 。
3. 单击 软件限制策略 。

注意 如果列出, 没有软件限制右键单击 软件限制策略 , 然后单击 新建策略
4. 在 对象类型 , 双击 强制 。
5. 单击 除本地管理员以外的所有用户 , 并单击 确定 。
6. 重新启动计算机。

for more infomation: http://support.microsoft.com/kb/925336

参考:
http://weblogs.asp.net/scottgu/archive/2006/12/15/visual-studio-2005-service-pack-1-sp1-released.aspx