c# - Web 应用程序项目 - 如何使用 ProfileCommon

标签 c# asp.net profilecommon

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

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

Popular folder structure for build

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

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

“找不到类型或 namespace 名称‘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();
}

网络配置:

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

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

感谢您的帮助和建议。

最佳答案

这是一篇很晚的帖子,但我在将 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();
    }

  }

}

关于c# - Web 应用程序项目 - 如何使用 ProfileCommon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1584507/

相关文章:

c# - 如果缺少 0 索引, Controller 将忽略列表属性

c# - 由于关键字为 "using"的未知命名空间,运行转换失败

asp.net dropdownlist 静态列表项

asp.net - ASP.NET 中自己的 OpenId 服务器的示例代码?

c# - HttpListener 类是否已弃用?

c# - 嵌套 foreach 作为 LINQ 与链式链表

c# - 将字符串设置到某个位置