为了我的一生,我无法让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>

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项目集成在一起?

先感谢您...

最佳答案

这是您需要做的:

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;
}

希望这可以帮助。

10-07 19:52
查看更多