views:

5546

answers:

6

I don't know what I am missing, but I added Profile properties in the Web.config file but cannot access Profile.Item in the code or create a new profile.

+2  A: 

If you are using a web application project, you cannot access the Profile object at design-time out-of-the-box. Here is a utility that supposedly does it for you: http://weblogs.asp.net/joewrobel/archive/2008/02/03/web-profile-builder-for-web-application-projects.aspx. Personally, that utility caused an error in my project so I ended up rolling my own profile class to inherit from ProfileBase. It was not hard to do at all.

nshaw
what namespace is profilebase in? cannot find. thanks
zsharp
System.Web.Profile - Here is some sample code: http://weblogs.asp.net/jgalloway/archive/2008/01/19/writing-a-custom-asp-net-profile-class.aspx
nshaw
+4  A: 

When you create a new Web site project in Visual Studio then the object that is returned from Profile will be (automatically) generated for you. When you create a Web application project or an MVC project, you will have to roll your own.

This probably sounds more difficult than it is. You need to do the following:

  • Create a database using aspnet_regsql.exe This tool is installed along with the .NET framework.
  • Write a class that derives from ProfileGroupBase or install the Web Profile Builder (WPB) that can generate the class for you from the definition in Web.Config. I have been using WPB for a while and up until now it has done what is expected of it. If you have a lot of properties, using WPB can save quite a bit of time.
  • Make sure the connection to the database is properly configured in Web.Config.
  • Now you are set to create an instance of your profile class (in the controller)
  • You will probably need the profile property values in your views. I like to pass the profile object itself along to the view (not individual properties).
Seventh Element
+41  A: 

I had the same problem today, and learned a lot.

There are two kinds of project in Visual Studio -- "Web Site Projects" and "Web Application Projects." For reasons which are a complete mystery to me, Web Application Projects cannot use Profile. directly... the strongly-typed class is not magically generated for you from the Web.config file, so you have to roll your own.

The sample code in MSDN assumes you are using a Web Site Project, and they tell you just to add a <profile> section to your Web.config and party on with Profile.property, but that doesn't work in Web Application Projects.

You have two choices to roll your own:

(1) Use the Web Profile Builder. This is a custom tool you add to Visual Studio which automatically generates the Profile object you need from your definition in Web.config.

I chose not to do this, because I didn't want my code to depend on this extra tool to compile, which could have caused problems for someone else down the line when they tried to build my code without realizing that they needed this tool.

(2) Make your own class that derives from ProfileBase to represent your custom profile. This is easier than it seems. Here's a very very simple example that adds a "FullName" string profile field:

In your web.config:

<profile defaultProvider="SqlProvider" inherits="YourNamespace.AccountProfile">

<providers>
     <clear />
     <add name="SqlProvider"
          type="System.Web.Profile.SqlProfileProvider"
          connectionStringName="sqlServerMembership" />
</providers>

</profile>

In a file called AccountProfile.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Profile;
using System.Web.Security;

namespace YourNamespace
{
    public class AccountProfile : ProfileBase
    {
        static public AccountProfile CurrentUser
        {
            get { return (AccountProfile)
                         (ProfileBase.Create(Membership.GetUser().UserName)); }
        }

        public string FullName
        {
            get { return ((string)(base["FullName"])); }
            set { base["FullName"] = value; Save(); }
        }

        // add additional properties here
    }
}

To set a profile value:

AccountProfile.CurrentUser.FullName = "Snoopy";

To get a profile value

string x = AccountProfile.CurrentUser.FullName;
Joel Spolsky
You just saved me hours of work!
Zaffiro
@joel so can I assume SO is using Profiles, and has proven to be scalable or was this research abandoned or intended for something else?
Simon_Weaver
Great answer, helped me too, I sort of had the same question.
Francisco Noriega
StackOverflow itself doesn't use profiles. This code is probably not fast enough for a site with 1,000,000 page views a day. I was working on another project when I wrote this.
Joel Spolsky
If you are implementing this and receive "Unable to cast object of type 'ProfileCommon' to type 'XYZ'", then see http://stackoverflow.com/questions/79129/implementing-profile-provider-in-asp-net-mvc. (I used 'ProfileCommon' instead of 'AccountProfile' as my class name).
Even Mien
You may also want to use [CustomProviderData] attribute.
majkinetor
It's my own fault, but I struggled for quite a while with setting properties after calling CurrentUser. Since CurrentUser instantiates and returns a new instance each time, can I suggest defining it as a method instead? Without thinking, I wrote this:AccountProfile.CurrentUser.FullName = "Snoopy";AccountProfile.CurrentUser.OtherProperty = "ABC";AccountProfile.CurrentUser.Save();which doesn't work. It should be:AccountProfile currentProfile = AccountProfile.CurrentUser;currentProfile.FullName = "Snoopy";currentProfile.OtherProperty = "ABC";currentProfile.Save();
Jeremy Gruenwald
Ummm... never mind.
Sky Sanders
Doesn't this kind of defeat the whole purpose of Profile? You can't use "Profile.MyProperty", you have to use your own home-grown class, which isn't set up on every page the way Profile automatically is. All this does is let you utilize the Save() feature without having to code it.
Mike at KBS
Web Application Projects cannot use Profile.
Joel Spolsky
A: 

The Web Profile Builder worked great for me. The class it generated has a lot more in it than as described by Joel's post. Whether or not its actually needed or useful I dont know.

Anyway for those looking for an easy way to generate the class, but not wanting to have an external build tool dependency you can always

  • use the web profile builder
  • delete all trace of it!
  • keep using the generated Profile class

OR (untested but may just work)

  • create a web site project
  • create your element
  • snap the generated class and copy it over to your web project project

if this second approach does work can someone let me know for future reference

Simon_Weaver
+1  A: 

MSDN walkthrough for creating a custom class (a.k.a. Joel's method):
http://msdn.microsoft.com/en-us/magazine/cc163624.aspx

Even Mien
A: 

Great post,

Just a note on the web.config if you dont specify the inherit attribute in the profile element you will need to specify each indiviudal profile property inside the profile element on the web.config as below

 <properties>
    <clear/>
    <add name="property-name-1" />
    <add name="property-name-2" />
    ..........

 </properties>
indy