[原创]web application中使用Profile应该注意的问题

转载请注明出处:菩提树下的杨过 http://blog.sqlsky.com
 
1.如何在web application中正确使用Profile
web application与website的一个不同之处在于,web application中无法象website中那样,直接用类似Label1.Text = Profile.XXX;这样的方式引用Profile(编译会直接报错)

解决办法有二种:
(1)
读取Profile值的代码改为:

1 HttpContext.Current.Profile.GetProfileGroup( " GroupName " ).GetPropertyValue( " PropertyName " );   // Profile有分组的情况 
2 HttpContext.Current.Profile.GetPropertyValue( " GroupName.PropertyName " );   // Profile有分组情况的另一种写法 
3 HttpContext.Current.Profile.GetPropertyValue( " PropertyName " );  // Profile无分组的情况 

修改Profile值的代码改为:

1 HttpContext.Current.Profile.SetPropertyValue( " GroupName.PropertyName " " Value " );  // 有分组情况
2 HttpContext.Current.Profile.SetPropertyValue( " PropertyName " " Value " );  // 无分组情况

保存

HttpContext.Current.Profile.Save();

缺点:这样虽然可以读取/修改/保存Profile,但这种写法把Profile降级为弱类型了,在vs.net开发环境中也失去了代码提示自动感知的能力

(2)推荐使用!利用Web Profile Builder生成强类型的Profile
步骤:
a.先到http://code.msdn.microsoft.com/WebProfileBuilder/Release/ProjectReleases.aspx?ReleaseId=674 这里下载这个免费的开源小插件,并安装
b.修改web.config文件,首先增加一个节点

< sectionGroup  name ="robo.webProfile" >
< section  name ="webProfileSettings"  type ="WebProfileBuilder.WebProfileConfigurationSection, WebProfileBuilder, Version=1.1.0.0, Culture=neutral, PublicKeyToken=01d50f1f82943b0c"  allowLocation ="true"  allowDefinition ="Everywhere" />
</ sectionGroup >  

把这一段复制到<configSections>这一行的后面,即

<? xml version="1.0" ?>
< configuration >
 
< configSections >
  
< sectionGroup  name ="robo.webProfile" >
   
< section  name ="webProfileSettings"  type ="WebProfileBuilder.WebProfileConfigurationSection, WebProfileBuilder, Version=1.1.0.0, Culture=neutral, PublicKeyToken=01d50f1f82943b0c"  allowLocation ="true"  allowDefinition ="Everywhere" />
  
</ sectionGroup >
  


然后再把

< robo.webProfile >
 
< webProfileSettings  className ="CntvsWebProfile"  directory ="App_Code"  fileName ="CntvsWebProfile" />
</ robo.webProfile >  

复制到</configSections>的下面,即


</ configSections >
< robo.webProfile >
 
< webProfileSettings  className ="CntvsWebProfile"  directory ="App_Code"  fileName ="CntvsWebProfile" />
</ robo.webProfile >


稍微解释一下,这一段告诉编译器,将在App_Code目录下生成一个CntvsWebProfile.cs的文件,类名为CntvsWebProfile(当然还可以指定namespace,具体可以参看WebProfileBuilder的sample),注意App_Code如果不存在将生成失败,另外最好在App_Code目录下,事先新建一个空的CntvsWebProfile.cs,否则好象也容易造成失败
c.关键!!!:修改项目文件xxx.csproj
退出vs.net,用记录本打开项目文件,找到 
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v9.0\WebApplications\Microsoft.WebApplication.targets" />
在这里再加上一行

< Import  Project ="$(MSBuildExtensionsPath)\WebProfileBuilder\WebProfileBuilder.targets"   />

这里就是告诉编译器,每次Build时,如果遇到web.config中的Profile设置有变化,将自动重新生成CntvsWebProfile.cs文件!!!
d.完成上述操作后,再次打开该项目,会提示该项目文件已经被修改,可能不安全之类的警告,不要理它,继续正常加载项目,Build一下,检查一下App_Code/CntvsWebProfile.cs的内容是否正确,如果正确的话,还要检查一下该cs文件的Property中的Build Action是否为Compile,如果不是,调整为Compile,否则别的地方没办法引用这个类

ok,终于完成了,下面再来看下如何使用这个cs类:
先给出web.config中的Profile配置节:

 1
 2 < anonymousIdentification  enabled ="true" />
 3 < profile  defaultProvider ="CNTVSWebSiteProfileProvider"  enabled ="true" >
 4   < providers >
 5    < add  name ="CNTVSWebSiteProfileProvider"  type ="System.Web.Profile.SqlProfileProvider"  connectionStringName ="ConnStr" />
 6   </ providers >
 7   < properties >
 8    < group  name ="AdminSystem" >
 9     < add  allowAnonymous ="true"  name ="Sex"  type ="String"  defaultValue ="Female" />
10     < add  allowAnonymous ="true"  name ="Age"  type ="Long" />
11     < add  allowAnonymous ="true"  name ="Name"  type ="String"  defaultValue ="unknown" />
12    </ group >
13   </ properties >
14 </ profile >  
15

 强类型使用Profile的示例代码:

 1 using  System;
 2 using  System.Collections;
 3 using  System.Configuration;
 4 using  System.Data;
 5 using  System.Linq;
 6 using  System.Web;
 7 using  System.Web.Profile;
 8 using  System.Web.Security;
 9 using  System.Web.UI;
10 using  System.Web.UI.HtmlControls;
11 using  System.Web.UI.WebControls;
12 using  System.Web.UI.WebControls.WebParts;
13 using  System.Xml.Linq;
14
15
16 namespace  Website
17 {
18    public partial class _Default : System.Web.UI.Page
19    {
20        public static CntvsWebProfile Profile
21        {
22            get return new CntvsWebProfile(HttpContext.Current.Profile); }
23        }

24
25        protected void Page_Load(object sender, EventArgs e)
26        {
27            ReadProfile();
28            SetProfile();
29        }

30
31
32        void ReadProfile() 
33        {
34            if (!IsPostBack)
35            {
36                txtAge.Text = Profile.AdminSystem.Age.ToString();
37                txtSex.Text = Profile.AdminSystem.Sex;
38                txtName.Text = Profile.AdminSystem.Name;
39            }

40        }

41
42        void SetProfile()
43        {
44            Profile.AdminSystem.Name = User.Identity.Name;
45            Profile.Save();
46        }

47
48        protected void btnSet_Click(object sender, EventArgs e)
49        {
50            Profile.AdminSystem.Age = Convert.ToInt16(txtAge.Text);
51            Profile.AdminSystem.Sex = txtSex.Text;
52            Profile.AdminSystem.Name = txtName.Text;
53            Profile.Save();
54        }

55    }

56}

57
58

代码很简单,除了要声明一个static的CntvsWebProfile外,其它跟website的使用方式完全一样

2.如何将一个匿名用户的Profile迁移到认证用户?
这种情况特别是在购物系统中很常见,比如浏览者在未登录的情况下,可以先把喜欢的商品加入基于Profile的购物车,要结算的时候再登录去付帐,默认情况下,匿名用户一旦登录成为认证用户,匿名状态下购物车中的东东将“丢失”,这里如果能把匿名用户的Profile迁移到认证用户就能避免该问题,解决办法:在Global.asax全局文件中处理,在全局文件中增加一个事件:Profile_MigrateAnonymous,代码参考下面  

//  将Profile值从匿名用户迁移到认证用户
protected    void  Profile_MigrateAnonymous(Object sender, ProfileMigrateEventArgs e)
{

    
//得到匿名用户的Profile
    CntvsWebProfile anonProfile = new CntvsWebProfile(System.Web.Profile.ProfileBase.Create(e.AnonymousID));

    
//得到当前通过验证的用户的Profile
    CntvsWebProfile Profile = new CntvsWebProfile(HttpContext.Current.Profile);

    
//将匿名用户的Profile赋值给认证用户
    Profile.AdminSystem.Age = anonProfile.AdminSystem.Age;
    Profile.AdminSystem.Name 
= anonProfile.AdminSystem.Name;
    Profile.AdminSystem.Sex 
= anonProfile.AdminSystem.Sex;
    
    
//删除匿名Profile
    ProfileManager.DeleteProfile(e.AnonymousID);
    AnonymousIdentificationModule.ClearAnonymousIdentifier();

    
//保存Profile
    Profile.Save();            
}
 

 

菩提树下的杨过 2008-4-12晚 整理于 上海

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值