【问题标题】:Web Application Project - how to use ProfileCommonWeb 应用程序项目 - 如何使用 ProfileCommon
【发布时间】:2009-10-18 09:58:58
【问题描述】:

我正在将我在旧机器上开发的网站移植到新的开发环境中。我没有复制所有文件,因为我没有很好的文件结构,并且在我进行过程中需要删除部分代码。

最初我创建了一个网站(文件 -> 新建 -> 网站)。我想要一个类似的文件结构:

Popular folder structure for build

所以我创建了一个新的空白解决方案,因此 sln 文件是独立的,然后添加了项目(各种 DLL 项目)和 ASP.NET Web 应用程序。

这最后一部分似乎给我带来了一些问题,我现在收到以下错误:

“找不到类型或命名空间名称‘ProfileCommon’”。

我找到了以下页面:

http://weblogs.asp.net/joewrobel/archive/2008/02/03/web-profile-builder-for-web-application-projects.aspx

这似乎有点啰嗦,我希望有人可能知道更好的解决方案。

我正在尝试将 ProfileCommon 与 CreateUser 向导一起使用,因为我在其中添加了一些额外的信息。

protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
    // Create an empty Profile for the newly created user
    ProfileCommon p = (ProfileCommon)ProfileCommon.Create(CreateUserWizard1.UserName, true);

    // Populate some Profile properties off of the create user wizard
    p.CurrentLevel = Int32.Parse(((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("clevel")).SelectedValue);

    // Save profile - must be done since we explicitly created it
    p.Save();
}

Web.config:

<profile enabled="true">
<properties>
    <add name="CurrentLevel" type="Int32"/>
</properties>
</profile>

如果有其他方法可以将此额外信息添加到创建向导中,或者只是为新用户设置额外信息的更好方法,那么我会全力以赴,非常感激。

感谢您的帮助和建议。

【问题讨论】:

    标签: c# asp.net profilecommon


    【解决方案1】:

    这是一篇很晚的帖子,但我在将 VB.NET Visual Studio 2008 (.NET 3.5) 网站移植到 C# Visual Studio 2010 (.NET 4.0) 网站时遇到了同样的问题。

    我在 MSDN 的 ProfileBase 文档中找到了对 ProfileCommon 的引用,但没有关于如何获取该对象的信息。

    从您有用的 MSDN 链接中,我注意到 ProfileCommon 永远只是 HttpContext 的包装器。

    简而言之,我使用var关键字从HttpContext中提取ProfileCommon信息,如下:

    var profile = HttpContext.Current.Profile;
    

    使用这一点信息,我能够创建整个类来为我的网站访问者读取和写入信息。

    和你一样,我希望这段代码可以帮助其他人:

    using System.Web;
    using System.Web.Security;
    
    namespace WebApplication17 {
    
      public partial class ManageProfile : System.Web.UI.Page {
    
        protected void Page_Load(object sender, EventArgs e) {
          if (!IsPostBack) {
            if (User.Identity.IsAuthenticated) {
              loadProfile();
            } else {
              goHome();
            }
          }
        }
    
        private void changePassword(string pwdOld, string pwdNew) {
          MembershipUser user = Membership.GetUser(User.Identity.Name);
          user.ChangePassword(pwdOld, pwdNew);
          Membership.UpdateUser(user);
        }
    
        private void goHome() {
          Server.Transfer("Default.aspx");
        }
    
        private void loadProfile() {
          MembershipUser user = Membership.GetUser(User.Identity.Name);
          txtEmail.Text = user.Email;
          TextBox3.Text = user.GetPassword();
          var profile = HttpContext.Current.Profile;
          txtTitle.Text = profile.GetPropertyValue("Title").ToString();
          txtName.Text = profile.GetPropertyValue("Name").ToString();
          txtAddress.Text = profile.GetPropertyValue("Address").ToString();
          txtCity.Text = profile.GetPropertyValue("City").ToString();
          txtSt.Text = profile.GetPropertyValue("St").ToString();
          txtZip.Text = profile.GetPropertyValue("Zip").ToString();
          txtPhone.Text = profile.GetPropertyValue("Phone").ToString();
          txtFax.Text = profile.GetPropertyValue("Fax").ToString();
          txtCompany.Text = profile.GetPropertyValue("Company").ToString();
        }
    
        private void setProfile() {
          MembershipUser user = Membership.GetUser(User.Identity.Name);
          user.Email = txtEmail.Text;
          Membership.UpdateUser(user);
          var profile = HttpContext.Current.Profile;
          profile.SetPropertyValue("Title", txtTitle.Text);
          profile.SetPropertyValue("Name", txtName.Text);
          profile.SetPropertyValue("Address", txtAddress.Text);
          profile.SetPropertyValue("City", txtCity.Text);
          profile.SetPropertyValue("St", txtSt.Text);
          profile.SetPropertyValue("Zip", txtZip.Text);
          profile.SetPropertyValue("Phone", txtPhone.Text);
          profile.SetPropertyValue("Fax", txtFax.Text);
          profile.SetPropertyValue("Company", txtCompany.Text);
          profile.Save();
        }
    
        protected void Button6_Click(object sender, EventArgs e) {
          changePassword(TextBox3.Text, TextBox4.Text);
          goHome();
        }
    
        protected void Button11_Click(object sender, EventArgs e) {
          setProfile();
          goHome();
        }
    
      }
    
    }
    

    【讨论】:

    • 其他查看者请注意:如果您只需要当前配置文件,这可能对您有用。如果您需要接收其他用户的配置文件,我认为 HttpContext 不会有帮助。如果您不知道,请说出来……
    • @Keith,使用ProfileBase.Create(string username) 函数获取特定用户的个人资料。
    • 这个解决方案的问题是它设置了当前的用户配置文件。它不会设置其他用户配置文件。
    【解决方案2】:

    我找到了解决这个问题的“一个”解决方案。不确定它是否是最好的,但它适用于我的情况。只需最少的代码更改。

    http://msdn.microsoft.com/en-us/library/aa983476.aspx

    希望它可以帮助别人,(或者当我再次忘记它时)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      • 2011-01-29
      • 1970-01-01
      • 2014-06-11
      • 2013-04-12
      • 2010-11-12
      • 1970-01-01
      相关资源
      最近更新 更多