在我的一些 aspx 页面上,我正在检查这样的会话
if (bool.Parse(Session["YourAssessment"].ToString()) == false
&& bool.Parse(Session["MyAssessment"].ToString()) == true)
{
Response.Redirect("~/myAssessment.aspx");
}
如果我经常玩这些页面,它工作正常,但如果我至少在 5 分钟内对页面不做任何事情,运行页面会引发错误
Object reference not set to an instance of an object.
以下是这个的堆栈
[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
有人可以向我解释这种奇怪的行为吗?
正如我们所知,默认情况下会话持续时间为 20 分钟。
已编辑
看到我有一个页面默认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;
}
}
在按钮点击它有
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");
}
在 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");
}
}
}
并在 yourAssessmtn 上像这样检查
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;
}
}
}
有什么问题请解释一下?