asp.net-mvc 在 ASP.NET MVC 中实现 Profile Provider

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/79129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 22:45:07  来源:igfitidea点击:

Implementing Profile Provider in ASP.NET MVC

asp.net-mvcprofileprovider

提问by senfo

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

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

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.

我意识到的第一个有趣的事情是 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"); 
}

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.

我遇到的问题是对 ProfileCommon.Create() 的调用无法转换为 ProfileCommon 类型,因此我无法取回我的配置文件对象,这显然会导致下一行失败,因为配置文件为空。

Following is a snippet of my web.config:

以下是我的 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.

MembershipProvider 工作顺利,所以我知道连接字符串很好。

Just in case it's helpful, here is my ProfileCommon class:

以防万一它有帮助,这是我的 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)));
        }
    }

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?

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

Thank you in advance...

先感谢您...

采纳答案by senfo

Here's what you need to do:

您需要执行以下操作:

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

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

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

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

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

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

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

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

Hope this helps.

希望这可以帮助。

回答by Mladen Mihajlovic

Not sure about the whole question, but one thing I noticed in your code:

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

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

You do not need both the (ProfileCommon) and the as ProfileCommon. They both do casts, but the () throws and exception while the as returns a null if the cast can't be made.

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

回答by Dave Dunkin

Try Web Profile Builder. It's a build script that automagically generates a WebProfile class (equivalent to ProfileCommon) from web.config.

尝试Web 配置文件生成器。它是一个构建脚本,可以从 web.config 自动生成一个 WebProfile 类(相当于 ProfileCommon)。

回答by Dave Dunkin

The web.config file in the MVC Beta is wrong. The SqlProfileProvider is in System.Web.Profile, not System.Web.Security. Change this, and it should start working for you.

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