简体   繁体   English

访问会话信息会给出System.NullReferenceException

[英]Accessing session info gives System.NullReferenceException

I'm bit new to ASP.net c# MVC. 我对ASP.net c#MVC有点陌生。 I'm trying to create a login application and create a session.In my application I have a login controller. 我正在尝试创建一个登录应用程序并创建一个会话。在我的应用程序中,我有一个登录控制器。 In the login controller I read logged in user's data to session variables like in the following code segment. 在登录控制器中,我将已登录用户的数据读取到会话变量中,如以下代码段所示。

[HttpPost]
public ActionResult Authorize(MVCFirst.Models.User userModel)
{
    using (MVCNewEntities db = new MVCNewEntities())
    {
        var userDetails = db.Users.Where(x => x.UserName == userModel.UserName && x.UserPWD == userModel.UserPWD).FirstOrDefault();
        if (userDetails == null)
        {
            userModel.LoginErrorMessage = "Incorrect useer name and password.";
            return View("Index", userModel);
        }
        else
        {
            Session["userID"] = userDetails.UserID;
            Session["userName"] = userDetails.UserName;
            return RedirectToAction("Index","Home");
        }
    }
}

My HomeController has Index which is an ActionResult . 我的HomeController Index是一个ActionResult In the View of the HomeController I try to read Session values of the session to html header like in the following code segment. HomeControllerView中,我尝试将会话的Session值读取为html标头,如以下代码段所示。

 <body> <div> <h1>Dashboard</h1> <h3>Username : @Session["userName"].ToString()</h3> <h3>User ID : @Session["userID"].ToString()</h3> <a href="@Url.Action("LogOut","Login")">Logout</a> </div> </body> 

When I compile the app it doesn't give any error. 当我编译应用程序时,它不会给出任何错误。 It build successful. 它建立成功。 But when I run it, it throws an exception. 但是当我运行它时,它会引发异常。 The message in the exception says the following. 异常中的消息说明以下内容。

Message "Object reference not set to an instance of an object." 消息“对象引用未设置为对象的实例。”

What I'm missing here? 我在这里想念的是什么? What's the mistake that I've done here? 我在这里犯了什么错误? Further explanation. 进一步解释。 This didn't occur when I try to login. 当我尝试登录时没有发生这种情况。 This occurred when I run the application for the first time. 当我第一次运行该应用程序时,会发生这种情况。

You can just omit the .ToString() . 您可以省略.ToString()

In MVC, doing @someObj will display nothing for null objects, and it will implicitly call .ToString() on anything else. 在MVC中,执行@someObj对于对象将不显示任何内容,并将对其他任何对象隐式调用.ToString()

<div> 
    <h1>Dashboard</h1>
    <h3>Username : @Session["userName"]</h3>
    <h3>User ID  : @Session["userID"]</h3>
</div>

just try to use this ( Object?.ToString() ) 只是尝试使用这个( Object?.ToString()

<body>
    <div> 
        <h1>Dashboard</h1>
        <h3>Username : @Session["userName"]?.ToString()</h3>
        <h3>User ID : @Session["userID"]?.ToString()</h3>
        <a href="@Url.Action("LogOut","Login")">Logout</a>
    </div>
</body>

In Global.Asax file set the folllowing 在Global.Asax文件中设置以下

protected void Application_PostAuthorizeRequest()
{
HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM