从 App_Code 访问 asp.net 配置文件
我正在创建一个基类,将从该基类派生特定页面。也就是说,它们不是从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您进入该页面时,您可以使用 ProfileCommon 类来访问配置文件。 profilecommon 类是在 Web 项目编译期间由 ASP.NET 根据您的 web.config 配置文件设置自动生成的。
如果您想使用 app_code 文件夹中的配置文件,则必须使用 profilebase 类。页面中可用的 Profilecommon 也派生自此类。
Profilebase 可以像这样访问
要读取配置文件值,您需要执行以下操作
要将值写入您需要写入的配置文件
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
To read a profile value you need to do the following
To write a value to the profile you need to write
您可以通过将其强制转换为 ProfileCommon 类型来访问强类型配置文件属性,如下所示:
You can access the strongly typed profile properties by casting it to the ProfileCommon type like this:
如果您想检索另一用户(而不是活动用户)的配置文件属性值,则可以使用以下方法:
您可以在代码隐藏中使用 ProfileCommon,但它在 App_Code 中不可用。
参考:MSDN
If you want to retrieve profile property values for another user (as opposed to the active one) this works:
You'd use ProfileCommon in code behind but it isn't available in App_Code.
Refernece: MSDN