解决办法有二种: 修改Profile值的代码改为: 保存 缺点:这样虽然可以读取/修改/保存Profile,但这种写法把Profile降级为弱类型了,在vs.net开发环境中也失去了代码提示自动感知的能力 (2)推荐使用!利用Web Profile Builder生成强类型的Profile 把这一段复制到<configSections>这一行的后面,即 复制到</configSections>的下面,即
1.如何在web application中正确使用Profile
web application与website的一个不同之处在于,web application中无法象website中那样,直接用类似Label1.Text = Profile.XXX;这样的方式引用Profile(编译会直接报错)
(1)
读取Profile值的代码改为:HttpContext.Current.Profile.GetProfileGroup("GroupName").GetPropertyValue("PropertyName"); //Profile有分组的情况
2HttpContext.Current.Profile.GetPropertyValue("GroupName.PropertyName"); //Profile有分组情况的另一种写法
3HttpContext.Current.Profile.GetPropertyValue("PropertyName"); //Profile无分组的情况
HttpContext.Current.Profile.SetPropertyValue("GroupName.PropertyName", "Value"); //有分组情况
2HttpContext.Current.Profile.SetPropertyValue("PropertyName", "Value"); //无分组情况
HttpContext.Current.Profile.Save();
步骤:
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>
<?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>
<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="
这里就是告诉编译器,每次Build时,如果遇到web.config中的Profile设置有变化,将自动重新生成CntvsWebProfile.cs文件!!!
d.完成上述操作后,再次打开该项目,会提示该项目文件已经被修改,可能不安全之类的警告,不要理它,继续正常加载项目,Build一下,检查一下App_Code/CntvsWebProfile.cs的内容是否正确,如果正确的话,还要检查一下该cs文件的Property中的Build Action是否为Compile,如果不是,调整为Compile,否则别的地方没办法引用这个类
ok,终于完成了,下面再来看下如何使用这个cs类:
先给出web.config中的Profile配置节:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
强类型使用Profile的示例代码:
12
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
代码很简单,除了要声明一个static的CntvsWebProfile外,其它跟website的使用方式完全一样
2.如何将一个匿名用户的Profile迁移到认证用户?
这种情况特别是在购物系统中很常见,比如浏览者在未登录的情况下,可以先把喜欢的商品加入基于Profile的购物车,要结算的时候再登录去付帐,默认情况下,匿名用户一旦登录成为认证用户,匿名状态下购物车中的东东将“丢失”,这里如果能把匿名用户的Profile迁移到认证用户就能避免该问题,解决办法:在Global.asax全局文件中处理,在全局文件中增加一个事件:Profile_MigrateAnonymous,代码参考下面
菩提树下的杨过 2008-4-12晚 整理于 上海
共同學習,寫下你的評論
評論加載中...
作者其他優質文章