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

OK,没有在VS2008中找到Profile的解决办法!<!-- In web.config -->
OK,没有在VS2008中找到Profile的解决办法!
<profile >
OK,没有在VS2008中找到Profile的解决办法!  
<properties>
OK,没有在VS2008中找到Profile的解决办法!    
<add name="jimmy" />
OK,没有在VS2008中找到Profile的解决办法!  
</properties>
OK,没有在VS2008中找到Profile的解决办法!
</profile>
OK,没有在VS2008中找到Profile的解决办法!

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

OK,没有在VS2008中找到Profile的解决办法!Profile.jimmy= "Pumpkin Ravioli";

然后~通过这种方式就跟Session一样:

OK,没有在VS2008中找到Profile的解决办法!<span id="user-favorite-pasta">
OK,没有在VS2008中找到Profile的解决办法!
<%= Profile.jimmy %>
OK,没有在VS2008中找到Profile的解决办法!
</span>

的确就是这么简单,我们通过这种方式就可以,如果你要问在VS2008行得通吗?
我只能告诉你NO !!!我找了一天了,没找到。。。我的确没有找到那熟悉的Profile,貌似就这么消失了。。

怎么办?只能通过自定义类来解决这个问题了。
以下这个类继承System.Web.Profile.ProfileBase,当然你也可以写你自己的。

OK,没有在VS2008中找到Profile的解决办法!using System.Web.Profile;
OK,没有在VS2008中找到Profile的解决办法!
using System.Web.Security;
OK,没有在VS2008中找到Profile的解决办法!
OK,没有在VS2008中找到Profile的解决办法!
namespace VideoShow
}

然后是web.config:
OK,没有在VS2008中找到Profile的解决办法!<!--profile中声明下来源自定义类-->
OK,没有在VS2008中找到Profile的解决办法!
<profile inherits="VideoShow.UserProfile">
OK,没有在VS2008中找到Profile的解决办法!  
<providers>
OK,没有在VS2008中找到Profile的解决办法!    
<clear />
OK,没有在VS2008中找到Profile的解决办法!    
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="VideoShowConnectionString"/>
OK,没有在VS2008中找到Profile的解决办法!  
</providers>
OK,没有在VS2008中找到Profile的解决办法!
</profile>

后台获取实例和得到属性:

OK,没有在VS2008中找到Profile的解决办法!UserProfile profile = serProfile.GetUserProfile(currentUser.UserName);
OK,没有在VS2008中找到Profile的解决办法!
//这里定义了一个TextBox FavoriteMovie
OK,没有在VS2008中找到Profile的解决办法!
profile.FavoriteMovie = FavoriteMovie.Text;
OK,没有在VS2008中找到Profile的解决办法!profile.Save();

当然还可以通过userName实例,在当前页中显示相应字段:
OK,没有在VS2008中找到Profile的解决办法!UserProfile profile = UserProfile.GetUserProfile(displayUser.UserName);
OK,没有在VS2008中找到Profile的解决办法!Response.Write(profile.FavoriteMovie)
最后是页面的数据绑定:
OK,没有在VS2008中找到Profile的解决办法!<span id="userFavoriteMovie">
OK,没有在VS2008中找到Profile的解决办法!
<%= VideoShow.UserProfile.GetUserProfile().FavoriteMovie %>
OK,没有在VS2008中找到Profile的解决办法!
</span>

写在最后:虽然这个办法在ASP.NET 2.0就有了,但是的确可以解决VS2008 中没有Profile的问题。

相关文章: