在ASP.NET MVC中实现Profile Provider

时间:2008-09-17 01:56:37

标签: asp.net-mvc profile provider

对于我的生活,我无法让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,因此我无法取回我的配置文件对象,这显然导致下一行失败,因为配置文件是空。

以下是我的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)));
        }
    }

对我可能做错了什么的任何想法?你有没有其他人成功地将ProfileProvider与你的ASP.NET MVC项目集成在一起?

提前谢谢你......

4 个答案:

答案 0 :(得分:56)

以下是您需要做的事情:

1)在Web.config的部分中,除了其他属性设置外,还要添加“inherits”属性:

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

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

3)将ProfileCommon.GetProfile()方法的代码更改为

public virtual ProfileCommon GetProfile(string username)        
{            
     return Create(username) as ProfileCommon;      
}

希望这有帮助。

答案 1 :(得分:6)

不确定整个问题,但我在你的代码中注意到了一件事:

ProfileCommon profile = (ProfileCommon)ProfileCommon.Create(user.UserName, user.IsApproved) as ProfileCommon;

您不需要(ProfileCommon)和ProfileCommon。它们都进行强制转换,但是()抛出和异常,而as如果无法进行强制转换则返回null。

答案 2 :(得分:6)

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

答案 3 :(得分:1)

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