[英]Web Application Project - how to use ProfileCommon
I am porting a site I had developed on an old box across to a new dev env. 我正在将我在旧盒子上开发的网站移植到新的开发环境中。 I have not just copied all the files as I didn't have a great file structure and some parts of the code needed to be removed as I went along.
我还没有复制所有文件,因为我没有一个很好的文件结构,并且我需要删除一些代码部分。
Originally I had created a website (File -> New -> Web Site). 最初我创建了一个网站(文件 - >新建 - >网站)。 I wanted a file structure something like:
我想要一个文件结构,如:
Popular folder structure for build 用于构建的流行文件夹结构
So I created a new blank solution so the sln file was on its own, then added projects (various DLL projects) and am ASP.NET Web Application. 所以我创建了一个新的空白解决方案,所以sln文件是独立的,然后添加项目(各种DLL项目)和ASP.NET Web应用程序。
This last part seems to have caused me a few issues, I am now getting the following error: 这最后一部分似乎给我带来了一些问题,我现在收到以下错误:
"The type or namespace name ' ProfileCommon' could not be found". “无法找到类型或命名空间名称'ProfileCommon'”。
I found the following page: 我找到了以下页面:
http://weblogs.asp.net/joewrobel/archive/2008/02/03/web-profile-builder-for-web-application-projects.aspx http://weblogs.asp.net/joewrobel/archive/2008/02/03/web-profile-builder-for-web-application-projects.aspx
It seems a bit long winded and I was hoping someone might know of a better solution. 这似乎有点长,我希望有人可能知道更好的解决方案。
I am trying to use the ProfileCommon with the CreateUser Wizard as I add a little extra information into it. 我试图将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: Web.config文件:
<profile enabled="true">
<properties>
<add name="CurrentLevel" type="Int32"/>
</properties>
</profile>
If there is another way to add this extra information into the creation wizard, or just a better way of setting extra info to a new user then I am all ears and would be very grateful. 如果有另一种方法可以将这些额外信息添加到创建向导中,或者只是为新用户设置额外信息的更好方法,那么我很满意并且非常感激。
Thanks for the help and advice. 感谢您的帮助和建议。
This is a very late post, but I just ran into this same problem when porting a VB.NET Visual Studio 2008 (.NET 3.5) website over to C# Visual Studio 2010 (.NET 4.0) website. 这是一个非常晚的帖子,但是当我将VB.NET Visual Studio 2008(.NET 3.5)网站移植到C#Visual Studio 2010(.NET 4.0)网站时,我遇到了同样的问题。
I found references to ProfileCommon
in MSDN's ProfileBase documentation, but nothing on how to get that object. 我发现引用
ProfileCommon
MSDN在ProfileBase文档,但没有就如何获取该对象。
From your helpful MSDN link, I noticed that ProfileCommon
would only ever be just a wrapper for the HttpContext
. 从您有用的MSDN链接中,我注意到
ProfileCommon
只会是HttpContext
的包装器。
In short, I used the var
keyword to extract the ProfileCommon
information from the HttpContext
, as in: 简而言之,我使用
var
关键字从HttpContext
提取ProfileCommon
信息,如下所示:
var profile = HttpContext.Current.Profile;
Using this one bit of information, I was able to create the entire class for reading and writing the information for my website visitors. 使用这一点信息,我能够创建整个类来读取和写入我的网站访问者的信息。
Like you, I hope this code might help someone else: 和你一样,我希望这段代码可以帮助别人:
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();
}
}
}
I found "a" solution to this one. 我发现了这个问题的“解决方案”。 Not sure if it is the best one or not but it worked for my situation.
不确定它是否是最好的,但它适用于我的情况。 Minimal code changes required.
需要最少的代码更改。
http://msdn.microsoft.com/en-us/library/aa983476.aspx http://msdn.microsoft.com/en-us/library/aa983476.aspx
Hope it might help someone else, (or me when I forget it again). 希望它可以帮助别人,(或者当我再次忘记它时)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.