当前上下文中不存在名称为ProfileCommon的内容。

3

我一直在浏览网页,但是没有找到任何有用的信息。我需要使用ProfileCommon,但是我无法引用任何程序集来使用它。有人可以帮帮我吗?

2个回答

4
如果你有一个ASP.NET网站(而不是应用程序项目)并且使用配置文件,那么ProfileCommon文件会在临时ASP.NET文件中自动生成。然而,当你使用ASP.NET项目时,你需要自己创建它。可以查看这个示例,了解如何自己实现。尽管该示例是为MVC应用程序项目设计的,但由于其基于ASP.NET本身,因此概念仍然相同。请注意保留HTML标记。

我没有使用MVC,并且我已经检查了类是否在ASP.Net临时文件夹中生成。无论如何,我有一个项目(由前任程序员开发),在该项目中有一行ProfileBase pb = ProfileCommon.GetUser(....);。它目前运行良好...但是我需要创建一个新项目,也使用ProfileCommon,但我找不到引用它的方法...请帮忙! - user384080
你不需要引用那个文件。如果你在一个新的网站中并且修改了web.config以使用Profile,那么它会默认自动生成。对于Web应用程序,你需要自己编写它。 - Kris van der Mast
你所说的“craft it”是什么意思? - user384080
@ronald:应该是创建。无论如何,你已经尝试过这个解决方案了吗? - Kris van der Mast
你试过我提供的链接中的示例吗?你只需要创建一个新类,在其中添加ProfileCommon所需的代码,正确设置属性并在web.config文件中相应地设置它。 - Kris van der Mast
如果该配置文件不存在,我需要创建它。但是ProfileBase.Create方法不会创建新的配置文件! - user384080

1

这是由框架生成的动态类型。在运行时,类型“ProfileCommon”存在。

如果您可以使用C# 4.0语言功能,则可以将ProfileCommon的行为封装在动态对象中。

我有以下扩展属性

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

使用动态对象的代码示例如下:

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

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();
        }
    }

请注意:此解决方案仅适用于检索当前配置文件。 - Keith Hoffman
@KeithHoffman 不要使用默认构造函数,而是使用重载。 - Sentient

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接