sen*_*nfo 63 provider profile asp.net-mvc
对于我的生活,我无法让SqlProfileProvider在我正在进行的MVC项目中工作.
我意识到的第一个有趣的事情是Visual Studio不会自动为您生成ProfileCommon代理类.这不是什么大问题,因为扩展ProfileBase类只是简单的事情.在创建了ProfileCommon类之后,我编写了以下用于创建用户配置文件的Action方法.
[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");
}Run Code Online (Sandbox Code Playgroud)
我遇到的问题是对ProfileCommon.Create()的调用无法转换为类型ProfileCommon,因此我无法取回我的配置文件对象,这显然导致下一行失败,因为配置文件为空.
以下是我的web.config的片段:
<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>
MembershipProvider正在顺利工作,所以我知道连接字符串是好的.
为了防止它有用,这是我的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)));
}
}Run Code Online (Sandbox Code Playgroud)
关于我可能做错什么的任何想法?你有没有其他人成功地将ProfileProvider与你的ASP.NET MVC项目集成在一起?
先感谢您...
小智 56
这是你需要做的:
1)在Web.config的部分中,除了您的其他属性设置外,还要添加"inherits"属性:
<profile inherits="MySite.Models.ProfileCommon" defaultProvider="....
Run Code Online (Sandbox Code Playgroud)
2)<properties>从Web.config中删除整个部分,因为您已经在自定义的ProfileCommon类中定义了它们,并且还指示在上一步中从您的自定义类继承它们
3)将ProfileCommon.GetProfile()方法的代码更改为
public virtual ProfileCommon GetProfile(string username)
{
return Create(username) as ProfileCommon;
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
不确定整个问题,但我在你的代码中注意到一件事:
ProfileCommon profile = (ProfileCommon)ProfileCommon.Create(user.UserName, user.IsApproved) as ProfileCommon;
Run Code Online (Sandbox Code Playgroud)
您不需要(ProfileCommon)和ProfileCommon.它们都进行强制转换,但是()抛出和异常,而as如果无法进行强制转换则返回null.
| 归档时间: |
|
| 查看次数: |
37855 次 |
| 最近记录: |