1.如何在web application中正确使用Profile

web application与website的一个不同之处在于,web application中无法象website中那样,直接用类似Label1.Text = Profile.XXX;这样的方式引用Profile(编译会直接报错)

解决办法有二种:

(1)

读取Profile值的代码改为:


web application中使用Profile应该注意的问题_项目文件web application中使用Profile应该注意的问题_项目文件_02代码 HttpContext.Current.Profile.GetProfileGroup("GroupName").GetPropertyValue("PropertyName"); //Profile有分组的情况

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

HttpContext.Current.Profile.GetPropertyValue("PropertyName"); //Profile无分组的情况



(2)推荐使用!利用Web Profile Builder生成强类型的Profile

步骤:

a.先到这里下载这个免费的开源小插件,并安装

b.修改web.config文件,首先增加一个节点


web application中使用Profile应该注意的问题_项目文件web application中使用Profile应该注意的问题_项目文件_02代码 <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>这一行的后面,即


web application中使用Profile应该注意的问题_项目文件web application中使用Profile应该注意的问题_项目文件_02代码 <?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>

web application中使用Profile应该注意的问题_ide_07 <webProfileSettings className="CntvsWebProfile" nameSpace="GenerateClass" directory="App_Code" fileName="CntvsWebProfile"/>

web application中使用Profile应该注意的问题_ide_07</robo.webProfile> 

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


</configSections>

web application中使用Profile应该注意的问题_ide_07<robo.webProfile>

web application中使用Profile应该注意的问题_ide_07 <webProfileSettings className="CntvsWebProfile" directory="App_Code" fileName="CntvsWebProfile"/>

web application中使用Profile应该注意的问题_ide_07</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配置节:


web application中使用Profile应该注意的问题_项目文件web application中使用Profile应该注意的问题_项目文件_02代码 <anonymousIdentification enabled="true"/>

<profile defaultProvider="CNTVSWebSiteProfileProvider" enabled="true">

<providers>

<add name="CNTVSWebSiteProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ConnStr"/>

</providers>

<properties>

<group name="AdminSystem">

<add allowAnonymous="true" name="Sex" type="String" defaultValue="Female"/>

<add allowAnonymous="true" name="Age" type="Long"/>

<add allowAnonymous="true" name="Name" type="String" defaultValue="unknown"/>

</group>

</properties>

</profile>




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


web application中使用Profile应该注意的问题_项目文件web application中使用Profile应该注意的问题_项目文件_02代码 using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Profile;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;



namespace Website

{

public partial class _Default : System.Web.UI.Page

{

public static CntvsWebProfile Profile

{

get { return new CntvsWebProfile(HttpContext.Current.Profile); }

}


protected void Page_Load(object sender, EventArgs e)

{

ReadProfile();

SetProfile();

}



void ReadProfile()

{

if (!IsPostBack)

{

txtAge.Text = Profile.AdminSystem.Age.ToString();

txtSex.Text = Profile.AdminSystem.Sex;

txtName.Text = Profile.AdminSystem.Name;

}

}


void SetProfile()

{

Profile.AdminSystem.Name = User.Identity.Name;

Profile.Save();

}


protected void btnSet_Click(object sender, EventArgs e)

{

Profile.AdminSystem.Age = Convert.ToInt16(txtAge.Text);

Profile.AdminSystem.Sex = txtSex.Text;

Profile.AdminSystem.Name = txtName.Text;

Profile.Save();

}

}

}


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

2.如何将一个匿名用户的Profile迁移到认证用户?

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


web application中使用Profile应该注意的问题_项目文件web application中使用Profile应该注意的问题_项目文件_02代码 // 将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();

}