Implementing Profile Provider in ASP.NET MVC

asp.net MvcProfileProfile Provider

asp.net Mvc Problem Overview


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

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




}

}

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.

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>

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

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)));
    }
}</code></pre>




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?




Thank you in advance...


asp.net Mvc Solutions


Solution 1 - asp.net Mvc

Here's what you need to do:

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

Solution 2 - asp.net Mvc

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.

Solution 3 - asp.net Mvc

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

Solution 4 - asp.net Mvc

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsenfoView Question on Stackoverflow
Solution 1 - asp.net MvcYogesh PrabhuView Answer on Stackoverflow
Solution 2 - asp.net MvcMladen MihajlovicView Answer on Stackoverflow
Solution 3 - asp.net MvcDave DunkinView Answer on Stackoverflow
Solution 4 - asp.net MvcSteve AndrewsView Answer on Stackoverflow