我如何使用Profilebase类?

时间:2010-05-22 18:01:30

标签: c# asp.net .net-3.5 visual-studio-2010

我尝试使用asp.net配置文件,我尝试使用ProfileBase

进行继承

public class dort_islem : ProfileBase
{
    public int ilk_sayi { get; set; }
    public int ikinci_sayi { get; set; }
}
  <anonymousIdentification enabled="true"/>
  <profile enabled="true" inherits="dort_islem">
   <providers>
    <clear/>
    <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="profiller"/>
   </providers>
   <properties>
    <add name="ad" allowAnonymous="true" defaultValue="yusuf"/>
    <add name="soy_ad" allowAnonymous="true"/>
    <add name="sevdigi_renk" allowAnonymous="true" type="System.Drawing.Color" serializeAs="Binary" />
        <group name="detaylar">
          <add name="numara" type="Integer" allowAnonymous="true"/>
          <add name="giris_tarihi" type="Date" allowAnonymous="true"/>
          <add name="cinsiyet" allowAnonymous="true"/>
          <add name="adres" allowAnonymous="true"/>
        </group>
   </properties>
  </profile>

但如果我尝试使用这些代码Default.aspx: alt text http://i47.tinypic.com/zn5v60.gif

我如何从dort_islem类中看到ilk_sayi?此致....

1 个答案:

答案 0 :(得分:10)

您缺少少数实施细节。

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部分中挂钩 - 请注意我在配置文件声明中包含了inherits =“VideoShow.UserProfile”:

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

完成后,我可以获取自定义配置文件类的实例并设置属性:

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

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