How to use Profile.GetProfile () in a library class?

I cannot figure out how to use the Profile.GetProfile() method in a library class. I tried using this method in the page.aspx.cs file and it worked perfectly.

How can I create a method that works in page.aspx.cs, work in a class library.

+4
source share
3 answers

In ASP.NET, the Profile is a hook to the HttpContext.Current.Profile property, which returns a dynamically generated ProfileCommon object obtained from System.Web.Profile.ProfileBase .

ProfileCommon apparently includes the GetProfile method (string name), but you will not find it officially registered in MSDN (and it will not appear in intellisense in visual studio) because most of the ProfileCommon class is dynamically generated when the ASP application .NET (the exact list of properties and methods will depend on how the "profiles" are configured in your web.config). GetProfile () is really mentioned on this MSDN page , so it seems real.

Perhaps the problem in your library class is that the configuration information from web.config is not being selected. Is your library class part of the Solultion that includes the web application, or are you just working on the library separately?

+2
source

You tried to add a link to System.Web.dll in your class library, and then:

 if (HttpContext.Current == null) { throw new Exception("HttpContext was not defined"); } var profile = HttpContext.Current.Profile; // Do something with the profile 
+1
source

You can use ProfileBase, but you are losing type safety. You can mitigate this with careful handling and error handling.

  string user = "Steve"; // The username you are trying to get the profile for. bool isAuthenticated = false; MembershipUser mu = Membership.GetUser(user); if (mu != null) { // User exists - Try to load profile ProfileBase pb = ProfileBase.Create(user, isAuthenticated); if (pb != null) { // Profile loaded - Try to access profile data element. // ProfileBase stores data as objects in a Dictionary // so you have to cast and check that the cast succeeds. string myData = (string)pb["MyKey"]; if (!string.IsNullOrWhiteSpace(myData)) { // Woo-hoo - We're in data city, baby! Console.WriteLine("Is this your card? " + myData); } } } 
0
source

All Articles