OK,我的确没有在VS2008中找到Profile,但是我找到了解决办法!

在VS2005中使用membership & profile 是一件很轻松的事情。随便添加一个字段:

<!-- In web.config -->
<profile >
  <properties>
    <add name="jimmy" />
  </properties>
</profile>

然后就那么简单,后台就能通过Profile拿到:

Profile.jimmy= "Pumpkin Ravioli";
然后~通过这种方式就跟Session一样:

<span id="user-favorite-pasta">
<%= Profile.jimmy %>
</span>
的确就是这么简单,我们通过这种方式就可以,如果你要问在VS2008行得通吗?
我只能告诉你NO !!!我找了一天了,没找到。。。我的确没有找到那熟悉的Profile,貌似就这么消失了。。

怎么办?只能通过自定义类来解决这个问题了。
以下这个类继承System.Web.Profile.ProfileBase,当然你也可以写你自己的。
当然还可以通过userName实例,在当前页中显示相应字段:


UserProfile profile = UserProfile.GetUserProfile(displayUser.UserName);
Response.Write(profile.FavoriteMovie)最后是页面的数据绑定:
<span id="userFavoriteMovie">
<%= VideoShow.UserProfile.GetUserProfile().FavoriteMovie %>
</span>
写在最后:虽然这个办法在ASP.NET 2.0就有了,但是的确可以解决VS2008 中没有Profile的问题。 

using System.Web.Profile;
using System.Web.Security;

namespace VideoShow
{
    public class UserProfile : ProfileBase
    {
        public static UserProfile GetUserProfile(string username)
        {
            return Create(username) as UserProfile;
        }
        public static UserProfile GetUserProfile()
        {
            return Create(Membership.GetUser().UserName) as UserProfile;
        }
        //匿名的不行
        [SettingsAllowAnonymous(false)]
        public string Description
        {
            get { return base["Description"] as string; }
            set { base["Description"] = value; }
        }

        [SettingsAllowAnonymous(false)]
        public string Location
        {
            get { return base["Location"] as string; }
            set { base["Location"] = value; }
        }

        [SettingsAllowAnonymous(false)]
        public string FavoriteMovie
        {
            get { return base["FavoriteMovie"] as string; }
            set { base["FavoriteMovie"] = value; }
        }
    }
}
然后是web.config:
<!--profile中声明下来源自定义类-->
<profile inherits="VideoShow.UserProfile">
  <providers>
    <clear />
    <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="VideoShowConnectionString"/>
  </providers>
</profile>
后台获取实例和得到属性:

UserProfile profile = serProfile.GetUserProfile(currentUser.UserName);
//这里定义了一个TextBox FavoriteMovie
profile.FavoriteMovie = FavoriteMovie.Text;
profile.Save();

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/octverve/archive/2008/03/24/2211485.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值