Pregunta

Por mi vida, no puedo hacer que SqlProfileProvider funcione en un proyecto MVC en el que estoy trabajando.

Lo primero interesante de lo que me di cuenta es que Visual Studio no genera automáticamente la clase de proxy ProfileCommon.Eso no es gran cosa ya que es simplemente una cuestión de extender la clase ProfileBase.Después de crear una clase ProfileCommon, escribí el siguiente método de acción para crear el perfil de usuario.

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

El problema que tengo es que la llamada a ProfileCommon.Create() no puede transmitirse al tipo ProfileCommon, por lo que no puedo recuperar mi objeto de perfil, lo que obviamente hace que la siguiente línea falle ya que el perfil es nulo.

A continuación se muestra un fragmento de mi 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 funciona sin problemas, por lo que sé que la cadena de conexión es buena.

En caso de que sea útil, aquí está mi clase 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)));
        }
    }

¿Alguna idea sobre lo que podría estar haciendo mal?¿Alguno de ustedes ha integrado exitosamente un ProfileProvider con sus proyectos ASP.NET MVC?

Gracias de antemano...

¿Fue útil?

Solución

Esto es lo que debes hacer:

1) En la sección de Web.config, agregue el atributo "hereda" además de sus otras configuraciones de atributos:

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

2) Retire todo <properties> sección de Web.config, ya que ya los ha definido en su clase personalizada ProfileCommon y también le ha indicado que herede de su clase personalizada en el paso anterior

3) Cambie el código de su método ProfileCommon.GetProfile() a

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

Espero que esto ayude.

Otros consejos

No estoy seguro de toda la pregunta, pero noté una cosa en su código:

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

No necesita tanto (ProfileCommon) como ProfileCommon.Ambos hacen conversiones, pero () lanza una excepción, mientras que as devuelve un valor nulo si no se puede realizar la conversión.

Intentar Creador de perfiles web.Es un script de compilación que genera automáticamente una clase WebProfile (equivalente a ProfileCommon) desde web.config.

El archivo web.config en MVC Beta es incorrecto.SqlProfileProvider está en System.Web.Profile, no en System.Web.Security.Cambie esto y debería empezar a funcionar para usted.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top