在ASP.NET MVC中实现configuration文件提供程序

对于我来说,我无法让SqlProfileProvider在我正在处理的MVC项目中工作。

我意识到的第一个有趣的事情是Visual Studio不会自动为您生成ProfileCommon代理类。 这不是什么大问题,因为扩展ProfileBase类很简单。 创build一个ProfileCommon类后,我编写了下面的Action方法来创build用户configuration文件。

[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()不能转换为inputProfileCommon,所以我无法取回我的configuration文件对象,这明显会导致下一行失败,因为configuration文件为空。

以下是我的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工作顺利,所以我知道连接string是好的。

为了以防万一,这里是我的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 MVC项目集成?

先谢谢你…

这是你需要做的:

1)在Web.config的部分,添加“inheritance”属性,除了你的其他属性设置:

 <profile inherits="MySite.Models.ProfileCommon" defaultProvider=".... 

2)从Web.config中删除整个<properties>部分,因为您已经在自定义ProfileCommon类中定义了它们,并且还指示在上一步中inheritance自定义类

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。 他们都做转换,但是()抛出和exception,而如果不能进行转换,返回null。

尝试Web Profile Builder 。 这是一个构build脚本,它自动从web.config中生成WebProfile类(相当于ProfileCommon)。

MVC Beta中的web.config文件是错误的。 SqlProfileProvider在System.Web.Profile中,而不是System.Web.Security。 改变这一点,它应该开始为你工作。