Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC:当前登录的用户-对象引用未设置为对象的实例_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# ASP.NET MVC:当前登录的用户-对象引用未设置为对象的实例

C# ASP.NET MVC:当前登录的用户-对象引用未设置为对象的实例,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我试图显示当前登录用户的信息。在AccountController中,我添加了以下操作方法: public class AccountController : Controller { private ApplicationSignInManager _signInManager; private ApplicationUserManager _userManager; public AccountController() { } public

我试图显示当前登录用户的信息。在
AccountController
中,我添加了以下操作方法:

public class AccountController : Controller
{
    private ApplicationSignInManager _signInManager;
    private ApplicationUserManager _userManager;

    public AccountController()
    {
    }

    public ApplicationUserManager UserManager
    {
        get
        {
            return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }

    //.......

    public ActionResult Profile()
    {
        var userId = User.Identity.GetUserId();

        if(userId == null)
        {
            return RedirectToAction("Login", "AccountControler");
        }
        else
        {
            ApplicationUser user = _userManager.FindByIdAsync(userId).Result;

            return View(user);
        }
    }
}
公共类AccountController:控制器
{
专用应用程序signInManager\u signInManager;
私有应用程序用户管理器\u用户管理器;
公共账户控制员()
{
}
公共应用程序管理员用户管理器
{
得到
{
返回_userManager??HttpContext.GetOwinContext().GetUserManager();
}
专用设备
{
_userManager=value;
}
}
//.......
公共行动结果概要文件()
{
var userId=User.Identity.GetUserId();
if(userId==null)
{
返回重定向到操作(“登录”、“AccountControler”);
}
其他的
{
ApplicationUser user=\u userManager.FindByIdAsync(userId).Result;
返回视图(用户);
}
}
}
但是,我得到以下例外情况:

对象引用未设置为对象的实例_userManager为空


_userManager将为null,除非您将其分配到某个位置(在您包含的代码中,您没有)

编辑,与您的编辑相对应

理想情况下,将UserManager实例传递到控制器构造函数中,并让依赖项注入处理它。您需要在Startup.cs中注册该服务

如果你还没准备好,你可能想浏览这个网站,寻找人们遇到类似问题的地方。“对象引用未设置为对象的实例”不是问题所在。问题出在UserManager getter内部

可能GetOwinContext()不可用。检查此问题和类似问题:

更好


学习使用调试器!在方法中放置一个断点并逐步执行,直到它中断。将鼠标悬停在变量上,查看哪个变量在您预期的情况下为空。

可能重复您忘记将
\u userManager
设置为某个值。userId获得正确的值_userManager为空。其余的只是由单个用户帐户功能生成的代码