在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"); }
我遇到的问题是对ProfileCommon.Create()的调用无法转换为ProfileCommon类型,因此我无法取回我的配置文件对象,这显然会导致下一行失败,因为profile为null。
以下是我的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> The MembershipProvider is working without a hitch, so I know that the connection string is good. Just in case it's helpful, here is my ProfileCommon class: 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))); } }
我们不需要(ProfileCommon)和as ProfileCommon。它们都进行强制类型转换,但是()会引发异常,而as如果无法进行强制类型转换,则返回null。
回答
尝试使用Web Profile Builder。这是一个构建脚本,可以从web.config自动生成一个WebProfile类(与ProfileCommon等效)。
回答
MVC Beta中的web.config文件错误。 SqlProfileProvider位于System.Web.Profile中,而不位于System.Web.Security中。更改它,它应该开始为我们工作。
回答
这是我们需要做的:
1)在Web.config的部分中,除了其他属性设置外,还添加" inherits"属性:
ProfileCommon profile = (ProfileCommon)ProfileCommon.Create(user.UserName, user.IsApproved) as ProfileCommon;
2)从Web.config中删除整个<properties>
部分,因为我们已经在自定义ProfileCommon类中定义了它们,并且还指示在上一步中从自定义类继承
3)将ProfileCommon.GetProfile()方法的代码更改为
<profile inherits="MySite.Models.ProfileCommon" defaultProvider="....
希望这可以帮助。
代码数量不匹配