开发者

ProfileCommon in WAP working at run-time but not at compile-time

I have a Web Application Project and implemented profile properties in the web.config. When I found t开发者_开发知识库hat 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?


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜