“The name ProfileCommon does not exist in the current context”

前端 未结 2 1797
北荒
北荒 2021-01-14 17:41

..been browsing the net but no luck.. I need to use the ProfileCommon but I can\'t reference any assemblies to use it.. can someone help?

相关标签:
2条回答
  • 2021-01-14 18:12

    When you have an ASP.NET web site, not application project, and make use of Profile the ProfileCommon file gets autogenerated in the temporary ASP.NET files. When you're using an ASP.NET project however you'll need to create that on your own. Take a look at this sample on how to implement it on your own. The sample is for usage in an MVC application project but since that's based on ASP.NET itself the concepts remain the same.

    0 讨论(0)
  • 2021-01-14 18:16

    This is a dynamic type generated by the framework. On runtime the type 'ProfileCommon' exists.

    If you can use the C# 4.0 language features you can wrap the behavior of ProfileCommon in a dynamic object.

    I have the following extended properties

    <profile enabled="true" >
          <properties>
            <add name="FirstName" type="String" />
            <add name="LastName" type="String" />
          </properties>
    </profile>
    

    The code example to use the dynamic object is

    dynamic profile = new ProfileData();
    var name = profile.FirstName + ' ' + profile.LastName;
    

    The implementation of ProfileData:

    public class ProfileData: DynamicObject
        {
            private readonly ProfileBase profileBase;
    
            /// <summary>
            /// Profile Data for the current user.
            /// </summary>
            public ProfileData()
            {
                profileBase = HttpContext.Current.Profile;
            }
    
            /// <summary>
            /// Profile data for user with name <paramref name="userName"/>
            /// </summary>
            /// <param name="userName"></param>
            public ProfileData(string userName)
            {
                profileBase = ProfileBase.Create(userName);         
            }
    
            // If you try to get a value of a property 
            // not defined in the class, this method is called.
            public override bool TryGetMember(GetMemberBinder binder, out object result)
            {
                try
                {
                    result = profileBase.GetPropertyValue(binder.Name);
                }
                catch(SettingsPropertyNotFoundException)
                {
                    result = null;
                    return false;
                }
                return true;            
            }
    
            // If you try to set a value of a property that is
            // not defined in the class, this method is called.
            public override bool TrySetMember(SetMemberBinder binder, object value)
            {
                try
                {
                    profileBase.SetPropertyValue(binder.Name, value);
                    return true;
                }
                catch (SettingsPropertyNotFoundException)
                {               
                    return false;
                }           
            }
    
            /// <summary>
            /// Persist the profile data.
            /// </summary>
            public void Save()
            {
                profileBase.Save();
            }
        }
    
    0 讨论(0)
提交回复
热议问题