开发者

Accessing asp.net Profiles from App_Code

开发者 https://www.devze.com 2022-12-09 12:21 出处:网络
I am creating a base class from which specific pages ar开发者_如何学编程e going to be derived. That is, instead of inheriting my pages from System.Web.UI.Page, they\'re inheriting from MyPage (which i

I am creating a base class from which specific pages ar开发者_如何学编程e going to be derived. That is, instead of inheriting my pages from System.Web.UI.Page, they're inheriting from MyPage (which in turn, inherits from System.Web.UI.Page).

However, I can't seem to be able to access the Profile property. Even though they both inherit from Page, I can only access the Profile properties if I'm at the actual page level.

I'm sure this is just my misunderstanding of the page life cycle, but is there a way to access the Profile from my custom class which is defined in App_Code?


When you are in the page you have the ProfileCommon class available to you for accessing the profile. The profilecommon class is automatically generated by asp.net from you web.config profile settings during compilation of the web project.

In case you want to use the profile from app_code folder, you will have to use the profilebase class. Profilecommon that is available in the page also derives from this class.

Profilebase can be access like this

HttpContext.Profile or HttpContext.Current.Profile

To read a profile value you need to do the following

HttpContext.Profile.GetPropertyValue("propertyName");

To write a value to the profile you need to write

HttpContext.Profile.SetPropertyValue("propertyName", "propertyValue");


You can access the strongly typed profile properties by casting it to the ProfileCommon type like this:

Dim Profile as ProfileCommon = CType(HttpContext.Current.Profile, ProfileCommon)

Profile.MyProperty1 = "Testing"
Profile.MyProperty2 = "Cool"


If you want to retrieve profile property values for another user (as opposed to the active one) this works:

Dim theUser As MembershipUser = Membership.GetUser(userID)
Dim theUserProfile As ProfileBase = ProfileBase.Create(theUser.UserName, True)
Dim theProperty As String = theUserProfile.GetPropertyValue("Property")

You'd use ProfileCommon in code behind but it isn't available in App_Code.

Refernece: MSDN

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号