实施文件提供程序在ASP.NET 视
-
09-06-2019 - |
题
为了我的生活,我不能获得SqlProfileProvider工作在视项目,该项目,我的工作。
第一件有趣的事情我才意识到的是,Visual Studio不会自动产生的ProfileCommon代理类。这不是一个大问题,因为它是这个问题的延伸ProfileBase类。在创建一个ProfileCommon类,我写了以下行动方法,用于创建的用户配置文件。
[AcceptVerbs("POST")]
public ActionResult CreateProfile(string company, string phone, string fax, string city, string state, string zip)
{
MembershipUser user = Membership.GetUser();
ProfileCommon profile = ProfileCommon.Create(user.UserName, user.IsApproved) as ProfileCommon;
profile.Company = company;
profile.Phone = phone;
profile.Fax = fax;
profile.City = city;
profile.State = state;
profile.Zip = zip;
profile.Save();
return RedirectToAction("Index", "Account");
}
这个问题,我有的是,呼吁ProfileCommon.Create()无法转换为类型ProfileCommon,所以我不能拿回我的资料对象,这显然导致下一个线到失败,因为档案是空的。
以下是一段代码我的网页。配置:
<profile defaultProvider="AspNetSqlProfileProvider" automaticSaveEnabled="false" enabled="true">
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ApplicationServices" applicationName="/" />
</providers>
<properties>
<add name="FirstName" type="string" />
<add name="LastName" type="string" />
<add name="Company" type="string" />
<add name="Phone" type="string" />
<add name="Fax" type="string" />
<add name="City" type="string" />
<add name="State" type="string" />
<add name="Zip" type="string" />
<add name="Email" type="string" >
</properties>
</profile>
该数是工作顺利,我知道,连串是好的。
只是在情况很有帮助,这里是我的ProfileCommon类:
public class ProfileCommon : ProfileBase
{
public virtual string Company
{
get
{
return ((string)(this.GetPropertyValue("Company")));
}
set
{
this.SetPropertyValue("Company", value);
}
}
public virtual string Phone
{
get
{
return ((string)(this.GetPropertyValue("Phone")));
}
set
{
this.SetPropertyValue("Phone", value);
}
}
public virtual string Fax
{
get
{
return ((string)(this.GetPropertyValue("Fax")));
}
set
{
this.SetPropertyValue("Fax", value);
}
}
public virtual string City
{
get
{
return ((string)(this.GetPropertyValue("City")));
}
set
{
this.SetPropertyValue("City", value);
}
}
public virtual string State
{
get
{
return ((string)(this.GetPropertyValue("State")));
}
set
{
this.SetPropertyValue("State", value);
}
}
public virtual string Zip
{
get
{
return ((string)(this.GetPropertyValue("Zip")));
}
set
{
this.SetPropertyValue("Zip", value);
}
}
public virtual ProfileCommon GetProfile(string username)
{
return ((ProfileCommon)(ProfileBase.Create(username)));
}
}
任何想法什么我可能做错了什么?有任何其余的你成功地综合了ProfileProvider你ASP.NET 视项目?
谢谢你提前...
解决方案
这里就是你需要做的:
1)在网。config的部分,添加"继承"的属性,在除了你其他属性设置:
<profile inherits="MySite.Models.ProfileCommon" defaultProvider="....
2)删除整个 <properties>
部分从网。config,因为你已经限定他们在你的定义ProfileCommon类和还指示要继承你的定义类在先前的步骤
3)更改密码ProfileCommon.GetProfile()方法
public virtual ProfileCommon GetProfile(string username)
{
return Create(username) as ProfileCommon;
}
希望这会有所帮助。
其他提示
不知道整个问题,但是有一件事我注意到你代码:
ProfileCommon profile = (ProfileCommon)ProfileCommon.Create(user.UserName, user.IsApproved) as ProfileCommon;
你不需要两(ProfileCommon)和作为ProfileCommon.他们都做演员,但()throws和例外而作为回报null如果转换不能作出。
尝试 网络配置文件生成器.这是一个建立脚本,自动产生WebProfile级(相当于ProfileCommon)从网。config。
网络。配置文件的视是错误的。该SqlProfileProvider是在系统。网。配置文件,不系统。网。安全。改变此,它应该开始为你工作。