前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[原创]web application中使用Profile应该注意的问题

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

作者头像
菩提树下的杨过
发布2018-01-22 17:14:28
8610
发布2018-01-22 17:14:28
举报
文章被收录于专栏:菩提树下的杨过

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

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

代码语言:javascript
复制
HttpContext.Current.Profile.GetProfileGroup("GroupName").GetPropertyValue("PropertyName");  //Profile有分组的情况 

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

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

修改Profile值的代码改为:

代码语言:javascript
复制
HttpContext.Current.Profile.SetPropertyValue("GroupName.PropertyName", "Value"); //有分组情况

HttpContext.Current.Profile.SetPropertyValue("PropertyName", "Value"); //无分组情况

保存

代码语言:javascript
复制
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文件,首先增加一个节点

代码语言:javascript
复制
<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>这一行的后面,即

代码语言:javascript
复制
<?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>

然后再把

代码语言:javascript
复制
<robo.webProfile>

 <webProfileSettings className="CntvsWebProfile" directory="App_Code" fileName="CntvsWebProfile"/>

</robo.webProfile>

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

代码语言:javascript
复制
</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,用记录本打开项目文件,找到 

代码语言:js
复制
 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
 <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v9.0\WebApplications\Microsoft.WebApplication.targets" />

在这里再加上一行

代码语言:javascript
复制
<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配置节:

代码语言:javascript
复制
<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的示例代码:

代码语言:javascript
复制
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,代码参考下面  

代码语言:javascript
复制
// 将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晚 整理于 上海

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2008-04-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档