1

I've been using ASP.NET MVC with membership-based login for several years and everything is working fine (it's now MVC3, but membership didn't change since the first version which was MVC1). Now I need to add a value into the profile (just one value, of very seldom use - so it doesn't warrant custom membership provider or custom tables)

I got it working through profile.SetPropertyValue("myprop"), but I would really want to get profile.myprop to work. Is it possible?

I saw some advise to have a custom class MyProfile : ProfileBase and have myprop as a property of that class. For some reason, casting ProfileBase.Create(currentUser.UserName) to MyProfile gives me an error (illegal cast).

Is there an example somewhere of ASP MVC application with Profile, similar to this Old Post by ScottGu?

Felix
  • 9,248
  • 10
  • 57
  • 89

1 Answers1

1

Joel Spolsky had a great answer to this question on this post. You're basically on the right track.

If you're getting the illegal cast error, its most likely due to a problem in the config file. Make sure this is included; more specifically the <profile defaultProvider="SqlProvider" inherits="YourNamespace.AccountProfile"> section.

<profile defaultProvider="SqlProvider" inherits="YourNamespace.AccountProfile">
    <providers>
         <clear />
         <add name="SqlProvider"
              type="System.Web.Profile.SqlProfileProvider"
              connectionStringName="sqlServerMembership" />
    </providers>
</profile>
Community
  • 1
  • 1
Bengel
  • 1,043
  • 7
  • 12
  • Well, that's exactly the post that I was referring to in my third paragraph :) But I am getting cast error when I am casting ProfileBase.Create to MyProfile class... – Felix Sep 29 '11 at 21:37
  • Yes, this is exactly what it was. Don't know how I missed "inherits" attribute in the original post. Probably, I was coding web.config from MSDN docs that don't apply to Web Projects. Additionally, profile properties should *only* be in the class; not in web config. Last, in my situation (which I think is common) I am populating profile properties as in Register action. Strangely, the user is **not** logged in at that time (even after `SetAuthCookie()` call. Therefore, rather than using static CurrentUser property, I created static `NamedUser(string userName)` function. – Felix Oct 01 '11 at 05:35