在ASP.NET MVC視圖我可以訪問個人資料和個人會員是這樣的:如何在ASP.NET MVC中的視圖中填充Profile變量?
<%= Profile.Name %> - <%= Profile.Karma %>
,但我如何才能填充形狀可變?它似乎在HttpContext.Current.Profile中。我已經寫了一個自定義配置文件類與CurrentUser方法,看起來像這樣:
static public Profile CurrentUser {
get {
return (Profile) (ProfileBase.Create(Membership.GetUser().UserName));
}
}
我有一個班指出,在我的web.config:
<profile inherits="MyProject.Models.Profile" defaultProvider="AspNetSqlProfileProvider" enabled="true">
<providers>
<clear />
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ApplicationServices" applicationName="/" />
</providers>
</profile>
此:
var p = CurrentUser.Profile
成功給我的個人資料,但我不能這樣設置:
HttpContext.Profile = CurrentUser.Profile;
因爲HttpContext.Profile是隻讀的。我得到的錯誤:
Error 18 Property or indexer 'System.Web.HttpContextBase.Profile' cannot be assigned to -- it is read only C:...\Controller.cs 26 17 MyProject
另外,我需要被分配給每個視圖配置文件,這樣就可以在通常的方式右上角會顯示登錄的用戶名。我應該怎麼做?
你永遠不會訪問那裏的個人資料。 – Pablo 2009-08-09 10:22:51
我可能會誤解你的目的,因爲你希望「登錄用戶的名字可以以通常的方式顯示在右上角」,我只是提供了我這樣做的方式。 – 2009-08-09 13:20:20
但這是用戶名,而不是名稱。 – Pablo 2009-08-09 21:54:40