代码之家  ›  专栏  ›  技术社区  ›  Jon

Web应用程序项目-如何使用profilecommon

  •  6
  • Jon  · 技术社区  · 16 年前

    我正在将一个我在旧盒子上开发的站点移植到一个新的dev env。我不仅复制了所有的文件,因为我没有一个很好的文件结构,而且在我进行的过程中需要删除一些代码部分。

    最初我创建了一个网站(文件->新建->网站)。我想要一个文件结构,比如:

    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>
    

    如果有其他方法可以将这些额外的信息添加到创建向导中,或者只是为新用户设置额外信息的更好方法,那么我将全神贯注,并非常感谢。

    谢谢你的帮助和建议。

    2 回复  |  直到 14 年前
        1
  •  5
  •   jp2code    14 年前

    这是一篇非常晚的文章,但我在将vb.net Visual Studio 2008(.NET 3.5)网站移植到C Visual Studio 2010(.NET 4.0)网站时遇到了同样的问题。

    我找到了 ProfileCommon 在MSDN中 ProfileBase 文档,但没有关于如何 得到 那个物体。

    从你有用的msdn链接,我注意到 普通配置文件 只不过是一个包装 HttpContext .

    总之,我用了 var 关键字提取 轮廓模型 信息来自 请求上下文 ,如:

    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();
        }
    
      }
    
    }
    
        2
  •  4
  •   Jon    16 年前

    我找到了解决这个问题的办法。不确定这是不是最好的,但它适用于我的情况。需要最少的代码更改。

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

    希望它能帮助别人(或者我再次忘记它的时候)。

    推荐文章