2009-08-08 42 views
0

在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

另外,我需要被分配給每個視圖配置文件,這樣就可以在通常的方式右上角會顯示登錄的用戶名。我應該怎麼做?

回答

0

我得到一個空名稱的原因並不是我沒有收到配置文件,而是數據庫中的配置文件實際上是空的(由於我的代碼中存在無關的錯誤)。我談到的所有事情都是讓個人資料運作所需要的。

-1

I need the Profile to be assigned for every view, so that the name of the logged in user can be displayed on the top right in the usual way

您可以創建一個登錄控件,並在控件中寫入。

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> 
<% 
    if (Request.IsAuthenticated) { 
%> 
     Welcome <b><%= Html.Encode(Page.User.Identity.Name) %></b>! 
     [ <%= Html.ActionLink("Log Off", "LogOff", "Account") %> ] 
<% 
    } 
    else { 
%> 
     [ <%= Html.ActionLink("Log On", "LogOn", "Account") %> ] 
<% 
    } 
%> 

HttpContext對象也有一個用戶屬性。

+0

你永遠不會訪問那裏的個人資料。 – Pablo 2009-08-09 10:22:51

+0

我可能會誤解你的目的,因爲你希望「登錄用戶的名字可以以通常的方式顯示在右上角」,我只是提供了我這樣做的方式。 – 2009-08-09 13:20:20

+0

但這是用戶名,而不是名稱。 – Pablo 2009-08-09 21:54:40

0

想必你已經在web.config中

<profile defaultProvider="MyCustomProfileProvider"> 
    <properties> 
    <!-- etc. --> 
    </properties> 
</profile> 

不確定MVC,但所需標準asp.net設置默認的配置文件提供。

+0

是的,我有。我會將其添加到問題中。 – Pablo 2009-08-09 11:36:34