woensdag, februari 07, 2007
ASP.NET : Profile
ASP.NET 2.0 has a 'profile' feature. I use it to store user preferences for both anonymous and registered visitors such as language preference and last page visited. Learn more about how to use them here.
Q: What is a profile and how is it used?
A: A profile is a set of values that ASP.NET will automatically store in its membership database. You define the values in the web.config file. If you need it to be available for anonymous users, you also need to add a section "anonymousIdentification". You can use the Profile object on your ASP.NET pages to access the properties, for example Profile.Culture in this case.

Q: How is this different from using the session object?
A: The usage is similar. But anything you store in the Session object is lost when the session is done. Whatever is stored in the Profile object will be remembered next time the user visits your site. So that is the base decision criterium: if you need to remember settings like language, add them to the profile. If it are temporary values stored during one visit, use Session.
Q: Why doesn't the Profile object work anymore with Visual Studio 2005 SP1 'Web Application Project'?
A: I spend a few hours figuring this out. If you will do like in the tutorial video in a new project, you might get compilation errors on the Profile object, and you will notice the IntelliSense on it doesn't work. The reason is that since SP1 there is a new kind of project for web applications. Most tutorials and books will be about the 'Web Site Project'. But some things change when you create the newly available 'Web Application Project'. For one, the Profile object doesn't get generated automatically anymore. Scott Guthrie explains it all in his blog. You can download a free program here, called ASP.NET Web Profile Generator. Install it. Then, in the solution explorer right-click the web.config file and select 'Generate Web Profile'. This will generate a new class WebProfile.cs. Now the Profile object works again. Yes!
I don't know if it is related, but I do get a timeout error on the Profile every time I run my application for the first time after starting Visual Studio 2005. After that it works fine. If that is the only problem, I can live with it.
PS: The App_Code folder also doesn't exist anymore in Web Application Projects. Datasets for example can be stored just as normal classes anywhere.
Q: What is a profile and how is it used?
A: A profile is a set of values that ASP.NET will automatically store in its membership database. You define the values in the web.config file. If you need it to be available for anonymous users, you also need to add a section "anonymousIdentification". You can use the Profile object on your ASP.NET pages to access the properties, for example Profile.Culture in this case.
Q: How is this different from using the session object?
A: The usage is similar. But anything you store in the Session object is lost when the session is done. Whatever is stored in the Profile object will be remembered next time the user visits your site. So that is the base decision criterium: if you need to remember settings like language, add them to the profile. If it are temporary values stored during one visit, use Session.
Q: Why doesn't the Profile object work anymore with Visual Studio 2005 SP1 'Web Application Project'?
A: I spend a few hours figuring this out. If you will do like in the tutorial video in a new project, you might get compilation errors on the Profile object, and you will notice the IntelliSense on it doesn't work. The reason is that since SP1 there is a new kind of project for web applications. Most tutorials and books will be about the 'Web Site Project'. But some things change when you create the newly available 'Web Application Project'. For one, the Profile object doesn't get generated automatically anymore. Scott Guthrie explains it all in his blog. You can download a free program here, called ASP.NET Web Profile Generator. Install it. Then, in the solution explorer right-click the web.config file and select 'Generate Web Profile'. This will generate a new class WebProfile.cs. Now the Profile object works again. Yes!
I don't know if it is related, but I do get a timeout error on the Profile every time I run my application for the first time after starting Visual Studio 2005. After that it works fine. If that is the only problem, I can live with it.
PS: The App_Code folder also doesn't exist anymore in Web Application Projects. Datasets for example can be stored just as normal classes anywhere.
Labels: asp.net

