如何在 ASP.NET MVC 的视图中填充 Profile 变量?

发布于 2024-07-30 19:49:52 字数 1302 浏览 5 评论 0原文

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

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

但是如何填充配置文件变量? 它似乎在 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>

This:

var p = CurrentUser.Profile

成功地为我提供了配置文件,但我无法像这样设置它:

HttpContext.Profile = CurrentUser.Profile;

因为 HttpContext.Profile 是只读。 我收到错误:

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

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

In a view in ASP.NET MVC I can access the profile and profile members like this:

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

but how do I get the Profile variable populated? It seems to be in HttpContext.Current.Profile. I've wrote a custom Profile class with a CurrentUser method that looks like this:

static public Profile CurrentUser {
    get {
        return (Profile) (ProfileBase.Create(Membership.GetUser().UserName));
    }
}

I have that class pointed to in my 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>

This:

var p = CurrentUser.Profile

successfully gives me the profile, but I cannot set it like this:

HttpContext.Profile = CurrentUser.Profile;

because HttpContext.Profile is read only. I get the error:

Error 18 Property or indexer 'System.Web.HttpContextBase.Profile' cannot be assigned to -- it is read only C:...\Controller.cs 26 17 MyProject

Also, 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. How should I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

素染倾城色 2024-08-06 19:49:52

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

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

不确定 MVC,但对于标准 asp.net 来说这是必需的。

Presumably you have set the default profile provider in web.config

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

Not sure about MVC, but for standard asp.net that is required.

猫九 2024-08-06 19:49:52

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

The reason why I was getting an empty name was not that I wasn't getting the profile, but that the profile in the database was effectively empty (due to an unrelated bug in my code). Everything I talked about was all that was needed to get the profile working.

掐死时间 2024-08-06 19:49:52

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

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

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

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

You can create a log on control, and in the control, write this.

<%@ 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 object has an user property as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文