0

在 ASP.NET MVC 的视图中,我可以像这样访问配置文件和配置文件成员:

<%= Profile.Name %> - <%= Profile.Karma %>

但是如何填充 Profile 变量?它似乎在 HttpContext.Current.Profile 中。我用 CurrentUser 方法编写了一个自定义 Profile 类,如下所示:

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 是只读的。我得到错误:

错误 18 无法将属性或索引器“System.Web.HttpContextBase.Profile”分配给 -- 它是只读的 C:...\Controller.cs 26 17 MyProject

另外,我需要为每个视图分配配置文件,以便登录用户的名称可以以通常的方式显示在右上角。我该怎么做?

4

3 回答 3

0

大概您已经在 web.config 中设置了默认配置文件提供程序

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

不确定 MVC,但对于需要的标准 asp.net。

于 2009-08-09T10:43:48.997 回答
0

我得到一个空名称的原因不是我没有得到配置文件,而是数据库中的配置文件实际上是空的(由于我的代码中的一个不相关的错误)。我所说的一切都是使配置文件正常工作所需要的一切。

于 2009-08-10T06:22:59.503 回答
-1

我需要为每个视图分配配置文件,以便登录用户的名称可以以通常的方式显示在右上角

您可以创建一个登录控件,并在控件中写入此内容。

<%@ 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 对象也有一个用户属性。

于 2009-08-09T02:47:00.550 回答