为什么会话对象抛出空引用异常?

Nov*_*Net 6 c# asp.net session

在我的一些aspx页面上,我正在检查这样的会话

if (bool.Parse(Session["YourAssessment"].ToString()) == false
    && bool.Parse(Session["MyAssessment"].ToString()) == true)
{
    Response.Redirect("~/myAssessment.aspx");
}
Run Code Online (Sandbox Code Playgroud)

如果我经常继续播放页面,它工作正常,但如果我至少在5分钟内没有对页面做任何事情,那么运行页面会引发错误

Object reference not set to an instance of an object.
Run Code Online (Sandbox Code Playgroud)

以下是此堆栈

[NullReferenceException: Object reference not set to an instance of an object.]
   yourAssessment.Page_Load(Object sender, EventArgs e) in d:\Projects\NexLev\yourAssessment.aspx.cs:27
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207
Run Code Online (Sandbox Code Playgroud)

有些人可以解释一下这种奇怪的行为吗?

正如我们所知,默认情况下会话持续时间为20分钟.

EDITED

看到我有一个页面默认的aspx,它有一个按钮修复了一些基础上重定向在默认页面它检查像这样

protected void Page_Load(object sender, EventArgs e)
{
    if (!HttpContext.Current.Request.IsAuthenticated)
    {
        Response.Redirect("~/login.aspx");
    }
    else
    {
        Session["YourAssessment"] = false;
        Session["MyAssessment"] = false;
    }
}
Run Code Online (Sandbox Code Playgroud)

按钮点击它有

protected void imgClientFreeEval_Click(object sender,
    System.Web.UI.ImageClickEventArgs e)
{
    if (HttpContext.Current.Request.IsAuthenticated)
    {
        string sqlQuery = "SELECT count(*) FROM SurveyClient WHERE UserID='"
            + cWebUtil.GetCurrentUserID().ToString() + "'";
        SqlParameter[] arrParams = new SqlParameter[0];
        int countSurvey = int.Parse(
            Data.GetSQLScalerVarQueryResults(sqlQuery).ToString());
        if (countSurvey > 0)
        {
            Session["YourAssessment"] = true;
            Session["MyAssessment"] = false;
        }
        Response.Redirect((countSurvey > 0)
            ? "~/yourAssessment.aspx"
            : "~/myAssessment.aspx");
    }
    else
    {
        Response.Redirect("~/login.aspx");
    }
Run Code Online (Sandbox Code Playgroud)

在myAssessment页面上,它会像这样检查

protected void Page_Load(object sender, EventArgs e)
{
    if (!HttpContext.Current.Request.IsAuthenticated)
    {
        Response.Redirect("~/login.aspx");
    }
    else
    {
        if (Session["YourAssessment"] != null
            && Session["MyAssessment"] != null
            && bool.Parse(Session["YourAssessment"].ToString())
            && !bool.Parse(Session["myAssessment"].ToString()))
        {
            Response.Redirect("~/yourAssessment.aspx");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

并在你的assessmtn上检查这样

protected void Page_Load(object sender, EventArgs e)
{
    if (!HttpContext.Current.Request.IsAuthenticated)
    {
        Response.Redirect("~/login.aspx");
    }
    else
    {
        if (Session["YourAssessment"] != null
            && Session["MyAssessment"] != null
            && !bool.Parse(Session["YourAssessment"].ToString())
            && bool.Parse(Session["MyAssessment"].ToString()))
        {
            Response.Redirect("~/myAssessment.aspx");
        }

        PopulateAllSurveyByUser();
        if (ViewState["surveyClientID"] != null)
        {
            grdSurveyDetail.Visible = true;
            PopulateSurveyDetails(
                int.Parse(ViewState["surveyClientID"].ToString()));
        }
        else
        {
            grdSurveyDetail.Visible = false;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有什么问题请解释一下?

Sha*_*hai 11

首先需要检查该会话变量是否存在

if(Session["YourAssessment"] != null)
    // Do something with it
else
    // trying to call Session["YourAssessment"].ToString() here will FAIL.
Run Code Online (Sandbox Code Playgroud)

发生这种情况,因为你的会话有一个生命周期,这意味着 - 它到期(定义它的cookie到期) - 因此你的对象消失了.您可以增加 sessionState timeoutweb.config以使会话持续更长时间.

例如,在web.config中

  <system.web>
      <sessionState timeout="40" />
  </system.web>
Run Code Online (Sandbox Code Playgroud)

只要客户端不清除它,并且Web服务器启动并运行,您的会话将持续40分钟.