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