在 ASP.NET MVC 中实现 Profile Provider [英] Implementing Profile Provider in ASP.NET MVC

查看:23
本文介绍了在 ASP.NET MVC 中实现 Profile Provider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一生中,我无法让 SqlProfileProvider 在我正在处理的 MVC 项目中工作.

For the life of me, I cannot get the SqlProfileProvider to work in an MVC project that I'm working on.

我意识到的第一个有趣的事情是 Visual Studio 不会自动为您生成 ProfileCommon 代理类.这没什么大不了的,因为它只是扩展 ProfileBase 类的问题.创建了 ProfileCommon 类后,我编写了以下 Action 方法来创建用户配置文件.

The first interesting thing that I realized is that Visual Studio does not automatically generate the ProfileCommon proxy class for you. That's not a big deal since it's simpy a matter of extending the ProfileBase class. After creating a ProfileCommon class, I wrote the following Action method for creating the user profile.

[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 类型,因此我无法取回我的配置文件对象,这显然会导致下一行失败,因为配置文件是空.

The problem that I'm having is that the call to ProfileCommon.Create() cannot cast to type ProfileCommon, so I'm not able to get back my profile object, which obviously causes the next line to fail since profile is null.

以下是我的 web.config 的一个片段:

Following is a snippet of my 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 工作顺利,所以我知道连接字符串是好的.

The MembershipProvider is working without a hitch, so I know that the connection string is good.

以防万一它有帮助,这是我的 ProfileCommon 类:

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

对我可能做错了什么有任何想法吗?其他人是否已成功将 ProfileProvider 与您的 ASP.NET MVC 项目集成?

Any thoughts on what I might be doing wrong? Have any of the rest of you successfully integrated a ProfileProvider with your ASP.NET MVC projects?

先谢谢你...

推荐答案

以下是您需要做的:

1) 在 Web.config 的部分中,除了您的其他属性设置之外,添加继承"属性:

1) In Web.config's section, add "inherits" attribute in addition to your other attribute settings:

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

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

2) Remove entire <properties> section from Web.config, since you have already defined them in your custom ProfileCommon class and also instructed to inherit from your custom class in previous step

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

3) Change the code of your ProfileCommon.GetProfile() method to

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

希望这会有所帮助.

这篇关于在 ASP.NET MVC 中实现 Profile Provider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆