2

在我的控制器类中,我有一个私有方法,它返回使用 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”需要对象引用

编译时显示错误。

4

3 回答 3

4

您正在从它们看起来在同一个类中的静态方法中调用非静态对象/属性。你需要有一个类的实例才能使用它。或将方法更改为非静态。

于 2013-02-16T20:35:14.800 回答
3

您正在以静态方法检索控制器的属性...

staticGetLoggedUser()方法中删除

从:

private static Account GetLoggedUser()
{
    // your code
}

至:

private Account GetLoggedUser()
{
    // your code
}
于 2013-02-16T20:38:13.237 回答
2

您无权访问静态方法中的基类实例成员。

于 2013-02-16T20:36:07.273 回答