Frage

Für das Leben von mir, ich kann nicht die SqlProfileProvider bekomme in einem MVC-Projekt zu arbeiten, die ich arbeite.

Die erste interessante Sache, die mir klar ist, dass Visual Studio nicht automatisch die für Sie Profile Proxy-Klasse generiert. Das ist kein großes Problem, denn es ist simpy darum, die Profile-Klasse erweitern. Nach einer Profil Klasse zu schaffen, schrieb ich die folgende Aktion Methode für das Benutzerprofil zu erstellen.

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

Das Problem, das ich habe, ist, dass der Aufruf von ProfileCommon.Create () kann nicht Profilegieße zu geben, so bin ich nicht in der Lage, mein Profil-Objekt zurück, das ist natürlich die nächste Zeile führt zum Scheitern verurteilt, da Profil null.

Es folgt ein Ausschnitt aus meinem 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>

Die MembershipProvider sind ohne Probleme arbeiten, damit ich weiß, dass die Verbindungszeichenfolge ist gut.

Für den Fall, es ist hilfreich, hier ist meine Profileklasse:

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

Alle Gedanken auf, was ich falsch sein könnte? Hat jemand von dem Rest von Ihnen erfolgreich integrierte ein Profil mit ASP.NET MVC Projekten?

Vielen Dank im Voraus ...

War es hilfreich?

Lösung

Hier ist, was Sie tun müssen:

1) In Web.config der Abschnitt, fügen Sie "erbt" -Attribut zusätzlich zu Ihren anderen Attributeinstellungen:

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

2) Abschnitt gesamten <properties> von Web.config, da sie sich bereits in Ihrer benutzerdefinierten Profilen Klasse definiert haben und auch aus Ihrer benutzerdefinierten Klasse in vorherigem Schritt erben instruiertet

3) Ändern Sie den Code Ihrer ProfileCommon.GetProfile () -Methode

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

Hope, das hilft.

Andere Tipps

über die ganze Frage nicht sicher, aber eine Sache, die ich in Ihrem Code bemerkt:

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

Sie brauchen nicht sowohl die (Profile) und die als Profile. Beide tun Abgüsse, aber die () throws und Ausnahme, während die als einen auf null, wenn die Umwandlung nicht vorgenommen werden kann.

Versuchen Sie Web Profile Builder . Es ist ein Build-Skript, das automatisch eine WebProfil Klasse (entspricht Profile) von web.config erzeugt.

Die Datei web.config in der MVC Beta ist falsch. Die SqlProfileProvider ist in System.Web.Profile, nicht System.Web.Security. Ändern Sie dies, und es sollte beginnen für Sie zu arbeiten.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top