1

我按照在线教程使用内置的 asp.net 分析器,但没有获取配置文件。每次运行 GetUserProfile 时它都返回 null:

这是我的调用代码:

        if (UserProfile.GetUserProfile() == null)
        {
            UserProfile.Create(Membership.GetUser().UserName);
        }
        UserProfile currentProfile = UserProfile.GetUserProfile();
        currentProfile.WorksAt = "WorksAt Test";
        currentProfile.Manages = "Manages Test";
        String WorksAt = currentProfile.WorksAt;
        String Manages = currentProfile.Manages;

这是我的 Profile 类的代码:

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

        public class UserProfile : ProfileBase
        {
            [SettingsAllowAnonymous(false)]
            public string WorksAt
            {
                get { return base["WorksAt"] as string; }
                set { base["WorksAt"] = value; }
            }

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

            public static UserProfile GetUserProfile(string username)
            {
                return Create(username) as UserProfile;
            }

            public static UserProfile GetUserProfile()
            {
                return Create(Membership.GetUser().UserName) as UserProfile;
            }
        }
4

1 回答 1

1

从在线教程中弄清楚,我需要在 Web.config 中定义配置文件提供程序时添加 inherits="UserProfile":

<profile defaultProvider="TestServerProfile" inherits="UserProfile">
        <providers>
            <clear/>
            <add name="TestServerProfile" type="System.Web.Profile.SqlProfileProvider" connectionStringName="TestServerConnection" applicationName="/"/>
        </providers>
  <properties>
    <group name="UserInformation">
      <add name="WorksAt" type="String" serializeAs="String" defaultValue=""  />
      <add name="Manages" type="String" serializeAs="String" defaultValue=""  />
    </group>
  </properties>
    </profile>
于 2012-05-10T15:35:48.267 回答