您如何在ASP.NET MVC中的视图中获取配置文件变量?
-
12-09-2019 - |
题
在ASP.NET MVC中的视图中,我可以访问这样的个人资料和个人资料成员:
<%= Profile.Name %> - <%= Profile.Karma %>
但是,如何获得个人资料变量人群?它似乎在httpcontext.current.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
另外,我需要为每个视图分配配置文件,以便以通常的方式可以在右上方显示登录用户的名称。我应该怎么做?
解决方案 2
我得到空名称的原因不是我没有得到配置文件,而是数据库中的配置文件有效地为空(由于我的代码中的错误是无关的错误)。我谈论的一切都是使个人资料正常工作所需的一切。
其他提示
大概您已经在web.config中设置了默认配置文件提供商
<profile defaultProvider="MyCustomProfileProvider">
<properties>
<!-- etc. -->
</properties>
</profile>
不确定MVC,但对于所需的标准ASP.NET。
我需要为每个视图分配配置文件,以便可以以通常的方式在右上方显示登录用户的名称
您可以在控件上创建登录,并在控件中写下此。
<%@ 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对象也具有用户属性。
不隶属于 StackOverflow