The name 'Profile' does not exist in the current context

在使用

ProfileCommon profile = Profile.GetProfile(myuser.ToString());

来取得一个用户的profile信息的时候,出现错误提示:The name 'Profile' does not exist in the current context

搜了一下

The Profile property that developers use is actually an instance property on each .aspx page class - the property name being "Profile". This is what allows developers to write Profile.MyFooProperty inside of a page, and things just magically work.

Once you are in a static method though, there is no page instance and thus you can't make use of the instance property called "Profile".

To get to an arbitrary profile from inside of a static method try this instead:

ProfileCommon muprofile = (ProfileCommon)ProfileBase.Create("username here",true);

This approach uses a static method on the base type of all ASP.NET profile instances to fetch a profile and then cast it to the autogenerated profile type.

所以,需要使用这句:

  1. ProfileCommon muprofile = (ProfileCommon)ProfileBase.Create("username here",true);
ProfileCommon muprofile = (ProfileCommon)ProfileBase.Create("username here",true);

就可以了

如果使用的是"ASP.NET web application" 而不是"web site",注意下面这个:

Visual Studio doesn't create the class on the fly when you're developing a "ASP.NET web application".

There is a add-in here(http://www.gotdotnet.com/workspaces/workspace.aspx?id=406eefba-2dd9-4d80-a48c-b4f135df4127). with this add-in you can create the class. just follow the instructions in the readme file.

关于Profile的一篇文章:

/* from: http://www.cnblogs.com/BoyXiao/archive/2010/03/02/1676414.html */

Profile 详解之 ProfileBase 基类探讨

 

为什么博文的取名为 ProfileBase 基类呢?ProfileBase 又是作为谁的父类呢?

事实上,这一篇博文将谈到三个类,

也即是 Profile 类, ProfileCommon 类, ProfileBase 类,

其中 Profile 类和 ProfileCommon 类都是派生自 ProfileBase 类,

所以文章的标题取名为 ProfileBase 基类。

上面说到 Profile 类和 ProfileCommon 类都是派生自 ProfileBase 类,

而事实上呢,Profile 类实质上就是一个 ProfileCommon 类(等下解释),

而 ProfileCommon 则是继承自 ProfileBase 类,

故才说 Profile 类和 ProfileCommon 类都是继承自 ProfileBase 基类,

经过了前面几篇博文中 Demo 的介绍,或多或少的也接触到了 ProfileCommon 类,

我想大家也差不多知道这个类大体是干什么用的了,

这个 ProfileCommon 类呢,

其大体的作用就是针对每一个特定的用户的 Profile 进行操作,

前面曾如下使用过

//首先是要实例化一个动态生成的类 ProfileCommon
//这个类代表了一个用户的 Profile

ProfileCommon userCommon =
Profile.GetProfile(ddlUserName.SelectedValue);

lblUser.Text = userCommon.UserName;
lblAddress.Text = userCommon.住址;
lblConstellate.Text = userCommon.星座;

从中可以看出我们先是通过一个 GetProfile() 的方法,

并且传入一个用户名得到的 ProfileCommon 类,

这里就可以知道这个 ProfileCommon 类是针对特定的用户来实例化的,

而后面的使用 ProfileCommon 来访问属性呢也可以表明其是操作单一用户的 Profile ,

同时经过了前面使用 Profile 类的使用,我们也大体知道,

要使用 Profile 这个类的话,我们必须登录成功后才能够使用(先不考虑匿名)

因为您必须告诉 Profile 这个类当前的用户是谁,

这样 Profile . Sava()才能够成功的保存,

不然其将不知道这些用户设置文件要给谁保存,

但是如果您登录成功了的话,

Profile类则会获取当前页面上的用户名(登录成功)来保存用户设置文件,

这样的话,Profile 是针对当前的登录成功的页面上的用户,

而 ProfileCommon 则是针对所有的用户,

但是一个 ProfileCommon 实例只对应一个用户,

从特殊性上看的话,Profile 是比 ProfileCommon 更加特殊的类了,

所以在这里也就不难理解为什么 Profile 实质上就是一个 ProfileCommon 类了,

但是上面的内容还不足以完全证明 Profile 就是一个 ProfileCommon 类,

而且由于 Profile 和 ProfileCommon 两个类都是动态生成的,MSDN 也查不了,

所以更不好证明,

但是下面可以使用调试来完成这个任务,

您可以在 Profile . [ 属性名 ] 上来设置断点,这样就可以查看 Profile 的一些类型信息了,

image

再启动调试,当运行到断点处时,

由于在这里无法截图(一失去焦点便看不见这些信息了),

所以大家必须自行尝试,但是我还是将一些信息给拷贝过来了的,

比如

Profile = {ProfileCommon}

base {System.Web.Profile.ProfileBase} = {ProfileCommon}

从这里边可以看出 Profile 的类型实质上就是一个 ProfileCommon ,

而 ProfileCommon 则是继承自 ProfileBase 类。

至于 ProfileCommon 继承自 ProfileBase 类的话,

您查 MSDN 也是可以查到的,不过前面的那一条您是差不到的!!!

下面是 MSDN 的说法

ASP.NET 使用 ProfileBase 类创建用于用户配置文件的类。

在启动启用了用户配置文件的应用程序时,

ASP.NET 会创建一个类型为 ProfileCommon 的新类,

该类从 ProfileBase 类继承。

强类型访问器被添加到 profile 配置节中为每个属性定义的 ProfileCommon 类中。

ProfileCommon 类的强类型访问器

调用 ProfileBase 基类的 GetPropertyValueSetPropertyValue 方法,

分别用于配置文件属性值的检索和设置。

ProfileCommon 类的一个实例被设置为 ASP.NET 应用程序的 Profile 属性的值。

在上面说了那么多,说实在的也不容易,只是希望各位真的能够理解各种机制,

而不是只会用而已。

这样,证明完 Profile 类就是一个 ProfileCommon 类,

而 ProfileCommon 类又是继承自 ProfileBase 类后,

下面就该介绍一下基类 ProfileBase 类了,

对于这个类的话,我想 MSDN 上面也说得够清楚了,

在这里我就只拷贝点方法过来算了,

大家可以自行去查阅更加详细的资料!!!

来自 MSDN

ProfileBase 类型公开了以下方法。

“折叠”图像方法

Create
已重载。 创建用户配置文件的实例。
Equals
确定指定的 Object 是否等于当前的 Object。 (继承自 Object。)
Finalize
允许 Object 在“垃圾回收”回收 Object 之前尝试释放资源并执行其他清理操作。

  1. (继承自 Object。)
    GetHashCode
    用作特定类型的哈希函数。 (继承自 Object。)
    GetProfileGroup
    获取按组名标识的属性组。
    GetPropertyValue
    获取配置文件属性的值。
    GetType
    获取当前实例的 Type。 (继承自 Object。)
    Initialize
    已重载。 初始化当前用户的配置文件属性值和信息。
    MemberwiseClone
    创建当前 Object 的浅表副本。 (继承自 Object。)
    Save
    用已更改的配置文件属性值更新配置文件数据源。

(重写 SettingsBase..::.Save()()()。)
SetPropertyValue
设置配置文件属性的值。
ToString
返回表示当前 Object 的 String。 (继承自 Object。)

那么现在大家就应该能够明白 Profile 类 , ProfileCommon 类 , ProfileBase 类 ,

之间到底是什么关系了,也该明白在什么情况下应该使用哪一个类来完成操作了。

原文地址:https://www.cnblogs.com/kofkyo/p/2594640.html