在VS2005中使用membership & profile 是一件很轻松的事情。随便添加一个字段:
<!-- In web.config -->
<profile >
<properties>
<add name="jimmy" />
</properties>
</profile>
<profile >
<properties>
<add name="jimmy" />
</properties>
</profile>
然后就那么简单,后台就能通过Profile拿到:
Profile.jimmy= "Pumpkin Ravioli";
然后~通过这种方式就跟Session一样:
<span id="user-favorite-pasta">
<%= Profile.jimmy %>
</span>
<%= Profile.jimmy %>
</span>
的确就是这么简单,我们通过这种方式就可以,如果你要问在VS2008行得通吗?
我只能告诉你NO !!!我找了一天了,没找到。。。我的确没有找到那熟悉的Profile,貌似就这么消失了。。
怎么办?只能通过自定义类来解决这个问题了。
以下这个类继承System.Web.Profile.ProfileBase,当然你也可以写你自己的。
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; }
}
}
}
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>
<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();
//这里定义了一个TextBox FavoriteMovie
profile.FavoriteMovie = FavoriteMovie.Text;
profile.Save();
当然还可以通过userName实例,在当前页中显示相应字段:
UserProfile profile = UserProfile.GetUserProfile(displayUser.UserName);
Response.Write(profile.FavoriteMovie)
最后是页面的数据绑定:Response.Write(profile.FavoriteMovie)
<span id="userFavoriteMovie">
<%= VideoShow.UserProfile.GetUserProfile().FavoriteMovie %>
</span>
<%= VideoShow.UserProfile.GetUserProfile().FavoriteMovie %>
</span>
写在最后:虽然这个办法在ASP.NET 2.0就有了,但是的确可以解决VS2008 中没有Profile的问题。