[英]How to use Razor Class Library from a Webforms Web Application Project
[英]Web Application Project - how to use ProfileCommon
我正在將我在舊盒子上開發的網站移植到新的開發環境中。 我還沒有復制所有文件,因為我沒有一個很好的文件結構,並且我需要刪除一些代碼部分。
最初我創建了一個網站(文件 - >新建 - >網站)。 我想要一個文件結構,如:
所以我創建了一個新的空白解決方案,所以sln文件是獨立的,然后添加項目(各種DLL項目)和ASP.NET Web應用程序。
這最后一部分似乎給我帶來了一些問題,我現在收到以下錯誤:
“無法找到類型或命名空間名稱'ProfileCommon'”。
我找到了以下頁面:
這似乎有點長,我希望有人可能知道更好的解決方案。
我試圖將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>
如果有另一種方法可以將這些額外信息添加到創建向導中,或者只是為新用戶設置額外信息的更好方法,那么我很滿意並且非常感激。
感謝您的幫助和建議。
這是一個非常晚的帖子,但是當我將VB.NET Visual Studio 2008(.NET 3.5)網站移植到C#Visual Studio 2010(.NET 4.0)網站時,我遇到了同樣的問題。
我發現引用ProfileCommon
MSDN在ProfileBase文檔,但沒有就如何獲取該對象。
從您有用的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();
}
}
}
我發現了這個問題的“解決方案”。 不確定它是否是最好的,但它適用於我的情況。 需要最少的代碼更改。
http://msdn.microsoft.com/en-us/library/aa983476.aspx
希望它可以幫助別人,(或者當我再次忘記它時)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.