How to use a ProfileProvider in a Web Application Project
Posted
Tuesday, May 15, 2007 2:21 PM
by
C-Dog's .NET Tip of the Day
So you finally got a project that is a good fit to use the ASP.NET 2.0 ProfileProvider for and you get all the entries needed in your web.config defined and now you are ready to use it. You open up the page where you intend to use your nice new profile class, start typing the letters P r o f only to realize the word Profile is not there. Why is this you wonder? You check the web.config and everything looks good, yet for some reason the Profile object is not there.
This is probably due to the fact that you are using a Web Application Project for whatever reason. The first thing to know about using WAPs is that they do not support dynamic compilation. The way the Profile object works behind the scens is by creating a ProfileCommon class dynamically using the properties you listed in the web.config. Since WAP doesn't support this, the Profile Object isn't there.
So what do you do? Obviously you can manually program against the ProfileInfo object, but who wants to do that. That removes half the fun of the new Profile functionality. Well after doing some research there is a workaround of sorts. There is a utility on gotdotnet (currently being phased out) called ASP.Net WebProfile Generator. This is a Visual Studio 2005 Add-in, that reads your properties in the Profile element of the Web.config and generates a class called WebProfile that you can use to access the Profile in a strongly typed manner. To create the class, simply right click on your Web.config and choose Generate WebProfile.
Imagine you have a Profile defined as the following.
<profile defaultProvider="MyProfileProvider">
<properties>
<add name="MyString" type="string" AllowAnonymous="true" />
<add name="MyInt" type="int32" AllowAnonymous="true" />
&/properties>
</profile>
In a web project, you would access the MyString property like this:
Profile.MyString = "Blah";
In a Web Application Project, you would access the MyString property like this:
WebProfile.Current.MyString = "Blah";
Here is the link download the ASP.NET Web Profile Generator.
ASP.NET Web Profile Generator
Read the complete post at http://www.dotnettipoftheday.com/blog.aspx?id=357