WAP 中的 ProfileCommon 在运行时工作,但在编译时不工作
我有一个 Web 应用程序项目并在 web.config 中实现了配置文件属性。当我发现无法访问 ProfileCommon
对象时,我在谷歌上搜索并找到了一些解决方法:
- 如何从会员提供商处获取用户/个人资料列表?
- 如何分配配置文件值?
- “名称 ProfileCommon 在当前上下文中不存在”
- http:// /forums.asp.net/t/1487719.aspx/1?profile+common+non+existant+in+ASP+NET+4+0+
- http:// weblogs.asp.net/jgalloway/archive/2008/01/19/writing-a-custom-asp-net-profile-class.aspx
等等。但没有人说我在运行时有一个 ProfileCommon
对象。这是我的示例:
<profile enabled="true">
<properties>
<add name="AdminRemark"/>
<add name="FacilityID" type="System.Int64" defaultValue="1"/>
</properties>
</profile>
此行编译良好且有效,但我有硬编码的属性名称:
rcbFacilityID.SelectedValue =
(ProfileBase.Create(userCurrent.UserName) as ProfileBase)
.GetPropertyValue("FacilityID").ToString();
但此行无法编译并给出错误: 无法找到类型或命名空间名称“ProfileCommon”(是否缺少使用指令或程序集引用?)):
rcbFacilityID.SelectedValue =
(ProfileBase.Create(userCurrent.UserName) as ProfileCommon)
.FacilityID.ToString();
但在运行时,我尝试在 Visual Studio Watch 窗口中执行该行,并且它有效!该事件将 ProfileBase.Create(userCurrent.UserName)
的类型显示为 ProfileCommon
而无需进行强制转换。智能感知不起作用,但可以检查该对象,我看到它定义了两个配置文件属性。
我不介意使用硬编码的配置文件属性名称,如果这是除自定义 ProfileCommon
类之外的唯一方法,但我想解释为什么 ProfileCommon
类是在运行时可用而不是在编译时可用?
I have a Web Application Project and implemented profile properties in the web.config. When I found that I couldn't access the ProfileCommon
object I googled and found some workarounds:
- How to Get List of User/Profile from Membership Provider?
- How to assign Profile values?
- "The name ProfileCommon does not exist in the current context"
- http://forums.asp.net/t/1487719.aspx/1?profile+common+non+existant+in+ASP+NET+4+0+
- http://weblogs.asp.net/jgalloway/archive/2008/01/19/writing-a-custom-asp-net-profile-class.aspx
And so on. But nobody said that I have a ProfileCommon
object at run-time. Here's my example:
<profile enabled="true">
<properties>
<add name="AdminRemark"/>
<add name="FacilityID" type="System.Int64" defaultValue="1"/>
</properties>
</profile>
This line compiles well and works, but I have the hard-coded property name:
rcbFacilityID.SelectedValue =
(ProfileBase.Create(userCurrent.UserName) as ProfileBase)
.GetPropertyValue("FacilityID").ToString();
But this line doesn't compile and gives the error: The type or namespace name 'ProfileCommon' could not be found (are you missing a using directive or an assembly reference?)):
rcbFacilityID.SelectedValue =
(ProfileBase.Create(userCurrent.UserName) as ProfileCommon)
.FacilityID.ToString();
But at run-time I tried to execute the line in the Visual Studio Watch window and it worked! It event displayed the type of ProfileBase.Create(userCurrent.UserName)
as ProfileCommon
without the cast. Intellisense didn't work, but a could inspect the object and I saw that it had both profile properties defined.
I don't mind working with hard-coded profile property names if it's the only way except the custom ProfileCommon
class, but I'd like an explanation why the ProfileCommon
class is available at run-time and not a compile-time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ProfileCommon 在编译时不可用,因为它是在应用程序启动时创建的类。这是由于您可以在 web.config 的配置文件元素内定义属性,以便可以动态添加它们。
请参阅官方文档中的备注。
ProfileCommon is not available at compile time because it is a class created when the application is started. This is due to the properties you can define inside the profile element in web.config so they can be dynamically added.
See the remarks in the official documentation.