继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

web application中使用Profile应该注意的问题

墨色风雨
关注TA
已关注
手记 173
粉丝 75
获赞 350

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

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

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

修改Profile值的代码改为:

1HttpContext.Current.Profile.SetPropertyValue("GroupName.PropertyName", "Value"); //有分组情况
2HttpContext.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"/>

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP