How can i use Profilebase class?

后端 未结 1 675
慢半拍i
慢半拍i 2020-12-19 19:08

i try to use asp.net profile, i try to give inheritance with ProfileBase


public class dort_islem : ProfileBase
{
    public int ilk_sayi { get; set; }
             


        
相关标签:
1条回答
  • 2020-12-19 19:50

    You are missing a few implementation details.

    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; }
            }
        }
    }
    

    Now we need to hook that up in the profile section of web.config - notice that I've included inherits="VideoShow.UserProfile" in the profile declaration:

    <profile inherits="VideoShow.UserProfile">
      <providers>
        <clear />
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="VideoShowConnectionString"/>
      </providers>
    </profile>
    

    With that done, I can grab an instance of the custom profile class and set a property:

    //Write to a user profile from a textbox value
    UserProfile profile = UserProfile.GetUserProfile(currentUser.UserName);
    profile.FavoriteMovie = FavoriteMovie.Text;
    profile.Save();
    

    from http://weblogs.asp.net/jgalloway/archive/2008/01/19/writing-a-custom-asp-net-profile-class.aspx

    0 讨论(0)
提交回复
热议问题