【发布时间】:2013-02-16 20:32:16
【问题描述】:
在我的控制器类中,我有一个私有方法,它返回使用 User.Identity.Name 作为参数获取的登录用户,这一切都很好。
private static Account GetLoggedUser()
{
AccountService accService = new AccountService();
Account userAccount = accService.GetAccountByUsername(User.Identity.Name);
return userAccount;
}
public ActionResult Edit()
{
var userAccount = GetLoggedUser();
...
}
问题是我在User.Identity.Name 线上收到此错误
非静态字段需要对象引用, 方法或属性 'System.Web.Mvc.Controller.User.get'
编译时显示错误。
【问题讨论】:
-
你能把
User.Identity.Name传进去吗?或者您可以使用PrincipalContect获取它吗?您需要传入当前用户,您可以在Page Level获取它,如果您的方法需要DomainName\\UserName中的用户名,则使用Split("\\")方法在命名并传递string[1]值