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

來源:互聯網
上載者:User

在使用

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.

 

所以,需要使用這句:

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 的一些類型資訊了,

再啟動調試,當運行到斷點處時,

由於在這裡無法截圖(一失去焦點便看不見這些資訊了),

所以大家必須自行嘗試,但是我還是將一些資訊給拷貝過來了的,

比如

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 基類的 GetPropertyValue 和 SetPropertyValue 方法,

分別用於設定檔屬性值的檢索和設定。

ProfileCommon 類的一個執行個體被設定為 ASP.NET 應用程式的 Profile 屬性的值。

在上面說了那麼多,說實在的也不容易,只是希望各位真的能夠理解各種機制,

而不是只會用而已。

這樣,證明完 Profile 類就是一個 ProfileCommon 類,

而 ProfileCommon 類又是繼承自 ProfileBase 類後,

下面就該介紹一下基類 ProfileBase 類了,

對於這個類的話,我想 MSDN 上面也說得夠清楚了,

在這裡我就只拷貝點方法過來算了,

大家可以自行去查閱更加詳細的資料。。。

來自 MSDN

ProfileBase 類型公開了以下方法。 方法

Create
已重載。 建立使用者設定檔的執行個體。
Equals
確定指定的 Object 是否等於當前的 Object。 (繼承自 Object。)
Finalize
允許 Object 在“記憶體回收”回收 Object 之前嘗試釋放資源並執行其他清理操作。 (繼承自 Object。)
GetHashCode
用作特定類型的雜湊函數。 (繼承自 Object。)
GetProfileGroup
擷取按組名標識的屬性群組。
GetPropertyValue
擷取設定檔屬性的值。
GetType
擷取當前執行個體的 Type。 (繼承自 Object。)
Initialize
已重載。 初始化目前使用者的設定檔屬性值和資訊。
MemberwiseClone
建立當前 Object 的淺表副本。 (繼承自 Object。)
Save
用已更改的設定檔屬性值更新設定檔資料來源。

(重寫 SettingsBase..::.Save()()()。)
SetPropertyValue
設定設定檔屬性的值。
ToString
返回表示當前 Object 的 String。 (繼承自 Object。)

那麼現在大家就應該能夠明白 Profile 類 , ProfileCommon 類 , ProfileBase 類 ,

之間到底是什麼關係了,也該明白在什麼情況下應該使用哪一個類來完成操作了。


 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.