如何在 ASP.NET MVC 的视图中填充 Profile 变量?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
大概您已在 web.config 中设置了默认配置文件提供程序
不确定 MVC,但对于标准 asp.net 来说这是必需的。
Presumably you have set the default profile provider in web.config
Not sure about MVC, but for standard asp.net that is required.
我得到空名称的原因并不是我没有获取配置文件,而是数据库中的配置文件实际上是空的(由于我的代码中存在不相关的错误)。 我所说的一切都是让配置文件正常工作所需的一切。
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.
您可以创建一个登录控件,并在控件中写入以下内容。
HttpContext 对象也有一个 user 属性。
You can create a log on control, and in the control, write this.
HttpContext object has an user property as well.