gpt4 book ai didi

c# - 将具有配置文件的网站转换为 Web 应用程序项目

转载 作者:太空狗 更新时间:2023-10-29 22:37:49 30 4
gpt4 key购买 nike

我正在尝试转换现有的 website到 Web 应用程序项目,我在让配置文件正常工作时遇到了很大的问题。

网站项目中代码隐藏的一个例子是

register-with-role-and-profile.ascx.cs

    // Add the newly created user to the default Role.
Roles.AddUserToRole(CreateUserWizard1.UserName, wsatDefaultRole);

// Create an empty Profile for the newly created user
ProfileCommon p = (ProfileCommon)ProfileCommon.Create(CreateUserWizard1.UserName, true);

// Populate some Profile properties. Values are located in web.config file
p.Company.Company = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbOfficeName")).Text;
p.Company.Address = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbOfficeAddress")).Text;
p.Company.City = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbOfficeCity")).Text;
p.Company.State = ((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("ddlStates")).SelectedValue;
p.Company.PostalCode = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbOfficeZip")).Text;
p.Company.Phone = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbContactPhone")).Text;
p.Company.Fax = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbContactFax")).Text;
p.Preferences.Newsletter = ((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("ddlNewsletter")).SelectedValue;

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

web.config

<profile defaultProvider="MyCMSTableProfileProvider" automaticSaveEnabled="false" enabled="true">
<providers>
<clear/>
<add name="MyCMSTableProfileProvider" applicationName="MyCMS" connectionStringName="dbMyCMSConnectionString" table="aspnet_CustomProfile" type="CustomProfile.SqlTableProfileProvider"/>
<add name="MyCMSStoredProcedureProfileProvider" applicationName="MyCMS" connectionStringName="dbMyCMSConnectionString" type="CustomProfile.SqlStoredProcedureProfileProvider" setProcedure="sp_wsat_SetCustomProfileData" readProcedure="sp_wsat_GetCustomProfileData"/>
</providers>
<properties>
<group name="Personal">
<add name="FirstName" type="String" defaultValue="[null]" customProviderData="FirstName;nvarchar"/>
<add name="LastName" type="String" defaultValue="[null]" customProviderData="LastName;nvarchar"/>
<add name="Gender" type="String" defaultValue="[null]" customProviderData="Gender;nvarchar"/>
<add name="BirthDate" type="DateTime" defaultValue="[null]" customProviderData="BirthDate;datetime"/>
<add name="Occupation" type="String" defaultValue="[null]" customProviderData="Occupation;nvarchar"/>
<add name="Website" type="String" defaultValue="[null]" customProviderData="PersonalWebsite;nvarchar"/>
</group>
<group name="Address">
<add name="Country" type="String" defaultValue="[null]" customProviderData="Country;nvarchar"/>
<add name="Address" type="String" defaultValue="[null]" customProviderData="Address;nvarchar"/>
<add name="AptNumber" type="String" defaultValue="[null]" customProviderData="AptNumber;nvarchar"/>
<add name="City" type="String" defaultValue="[null]" customProviderData="City;nvarchar"/>
<add name="State" type="String" defaultValue="[null]" customProviderData="State;nvarchar"/>
<add name="PostalCode" type="String" defaultValue="[null]" customProviderData="PostalCode;nvarchar"/>
</group>
<group name="Contacts">
<add name="DayPhone" type="String" defaultValue="[null]" customProviderData="DayPhone;nvarchar"/>
<add name="DayPhoneExt" type="String" defaultValue="[null]" customProviderData="DayPhoneExt;nvarchar"/>
<add name="EveningPhone" type="String" defaultValue="[null]" customProviderData="EveningPhone;nvarchar"/>
<add name="EveningPhoneExt" type="String" defaultValue="[null]" customProviderData="EveningPhoneExt;nvarchar"/>
<add name="CellPhone" type="String" defaultValue="[null]" customProviderData="CellPhone;nvarchar"/>
<add name="Fax" type="String" defaultValue="[null]" customProviderData="Fax;nvarchar"/>
</group>
<group name="Company">
<add name="Company" type="String" defaultValue="[null]" customProviderData="Company;nvarchar"/>
<add name="Address" type="String" defaultValue="[null]" customProviderData="Address2;nvarchar"/>
<add name="City" type="String" defaultValue="[null]" customProviderData="City2;nvarchar"/>
<add name="State" type="String" defaultValue="[null]" customProviderData="State2;nvarchar"/>
<add name="PostalCode" type="String" defaultValue="[null]" customProviderData="PostalCode2;nvarchar"/>
<add name="Phone" type="String" defaultValue="[null]" customProviderData="Phone2;nvarchar"/>
<add name="Fax" type="String" defaultValue="[null]" customProviderData="Fax2;nvarchar"/>
<add name="Website" type="String" defaultValue="[null]" customProviderData="Website2;nvarchar"/>
</group>
<group name="Preferences">
<add name="Culture" type="String" defaultValue="en-US" customProviderData="Culture;nvarchar"/>
<add name="Newsletter" type="String" defaultValue="[null]" customProviderData="Newsletter;nvarchar"/>
</group>
</properties>
</profile>

但这会产生错误找不到类型或命名空间名称“ProfileCommon”(您是否缺少 using 指令或程序集引用?)

使用这个 example我创建了 2 个新类

ProfileInfo.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace myWSAT.controls
{
[Serializable]
public class Personal
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public DateTime BirthDate { get; set; }
public string Occupation { get; set; }
public string Website { get; set; }
}

[Serializable]
public class Address
{
public string Country { get; set; }
public string Address1 { get; set; }
public string AptNumber { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
}

[Serializable]
public class Contacts
{
public string DayPhone { get; set; }
public string DayPhoneExt { get; set; }
public string EveningPhone { get; set; }
public string EveningPhoneExt { get; set; }
public string CellPhone { get; set; }
public string Fax { get; set; }

}

[Serializable]
public class Company
{
public string CompanyName { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
public string Website { get; set; }

}

[Serializable]
public class Preferences
{
public string Culture { get; set; }
public string Newsletter { get; set; }

}

[Serializable]
public class ProfileInfo
{
public Personal Personal { get; set; }
public Address Address { get; set; }
public Contacts Contacts { get; set; }
public Company Company { get; set; }
public Preferences Preferences { get; set; }
}

}

wProfile.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Profile;

namespace myWSAT.controls
{
public class wProfile : ProfileBase
{
public ProfileInfo ProfileInfo
{
get { return (ProfileInfo)GetPropertyValue("ProfileInfo"); }
}

public static wProfile GetProfile()
{
return (wProfile)HttpContext.Current.Profile;
}

public static wProfile GetProfile(string userName)
{
return (wProfile)Create(userName);
}
}
}

然后修改register-with-role-and-profile.ascx.cs

// add newly created user to default Role specified above
if (Roles.RoleExists(wsatDefaultRole))
{
// Add the newly created user to the default Role.
Roles.AddUserToRole(CreateUserWizard1.UserName, wsatDefaultRole);

// Create an empty Profile for the newly created user
wProfile p = wProfile.GetProfile(Membership.GetUser().UserName);
//ProfileCommon p = (ProfileCommon)ProfileCommon.Create(CreateUserWizard1.UserName, true);

// Populate some Profile properties. Values are located in web.config file
p.ProfileInfo.Company.CompanyName = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbOfficeName")).Text;
p.ProfileInfo.Company.Address = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbOfficeAddress")).Text;
p.ProfileInfo.Company.City = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbOfficeCity")).Text;
p.ProfileInfo.Company.State = ((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("ddlStates")).SelectedValue;
p.ProfileInfo.Company.PostalCode = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbOfficeZip")).Text;
p.ProfileInfo.Company.Phone = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbContactPhone")).Text;
p.ProfileInfo.Company.Fax = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbContactFax")).Text;
p.ProfileInfo.Preferences.Newsletter = ((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("ddlNewsletter")).SelectedValue;

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

}

此构建和运行正常,但以下行始终返回 null。

wProfile p = wProfile.GetProfile(Membership.GetUser().UserName);

我不确定哪里出了问题?我还尝试了此 link 底部的示例没有成功

编辑:

我已经阅读了很多链接并尝试了几种解决方案,但我的专业知识是这个领域不是很好。我想我是在寻求一些语法方面的帮助来让它运行,我可能会在可能的情况下提供赏金。

编辑:如果我附上我尝试将其转换为 WAP 的当前状态,它可能会有所帮助。 http://www.mediafire.com/?ouabeoxwu75b52c

最佳答案

一个问题是您正在调用 Membership.GetUser() 重载,它将加载当前登录用户的用户详细信息。由于这似乎是用户可以注册他/她的页面的一部分,这意味着代码运行时没有当前登录的用户。

您可能应该将行更改为:

wProfile p = wProfile.GetProfile(Membership.GetUser(CreateUserWizard1.UserName).UserName);

这是假设您此时已经创建了用户。或者更简单:

wProfile p = wProfile.GetProfile(CreateUserWizard1.UserName);

但我建议您也更改 wProfile 类,以便 GetProfile(string userName) 加载并返回用户名为 的用户的配置文件用户名。然后添加一个明确的 CreateProfile(string userName) 方法,您可以在要创建配置文件时使用该方法。在我看来,创建一个新的配置文件作为名为 GetProfile 的方法的副作用并不是一个好主意。

代码可以是:

wProfile p = wProfile.CreateProfile(CreateUserWizard1.UserName);

关于c# - 将具有配置文件的网站转换为 Web 应用程序项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18475363/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com