从 App_Code 访问 asp.net 配置文件

发布于 2024-08-08 11:40:58 字数 240 浏览 1 评论 0原文

我正在创建一个基类,将从该基类派生特定页面。也就是说,它们不是从 System.Web.UI.Page 继承我的页面,而是从 MyPage 继承(而 MyPage 又从 System.Web.UI.Page 继承)。

但是,我似乎无法访问 Profile 属性。尽管它们都继承自 Page,但我只能在实际页面级别访问配置文件属性。

我确信这只是我对页面生命周期的误解,但是有没有办法从 App_Code 中定义的自定义类访问配置文件?

I am creating a base class from which specific pages are 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

知足的幸福 2024-08-15 11:40:58

当您进入该页面时,您可以使用 ProfileCommon 类来访问配置文件。 profilecommon 类是在 Web 项目编译期间由 ASP.NET 根据您的 web.config 配置文件设置自动生成的。

如果您想使用 app_code 文件夹中的配置文件,则必须使用 profilebase 类。页面中可用的 Profilecommon 也派生自此类。

Profilebase 可以像这样访问

HttpContext.Profile or HttpContext.Current.Profile

要读取配置文件值,您需要执行以下操作

HttpContext.Profile.GetPropertyValue("propertyName");

要将值写入您需要写入的配置文件

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

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");
つ低調成傷 2024-08-15 11:40:58

您可以通过将其强制转换为 ProfileCommon 类型来访问强类型配置文件属性,如下所示:

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

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

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"
来日方长 2024-08-15 11:40:58

如果您想检索另一用户(而不是活动用户)的配置文件属性值,则可以使用以下方法:

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

您可以在代码隐藏中使用 ProfileCommon,但它在 App_Code 中不可用。

参考:MSDN

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文