会话到期并抛出异常

use*_*084 3 c# asp.net session

我遇到会话到期问题.首先它每20分钟到期,它会抛出一个错误......

我试图修复它:

if (Session["userName"].ToString() == null)
{
   Session.RemoveAll();
   Response.Redirect("~/Login.aspx?sessionError=" + "*Session Expired on pageload PleaseLog in again");
}
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

你调用的对象是空的.

我的堆栈跟踪是:

[NullReferenceException:对象引用未设置为对象的实例.] c:\ Users\jagmit\Documents\Visual Studio 2008\Projects\copiunGUI\copiunGUI\Site1.Master.cs:224 copiunGUI中的copiunGUI.Site1.checksession() .Site1.TreeViewMain_Unload(Object sender,EventArgs e)在C:\ Users\jagmit\Documents\Visual Studio 2008\Projects\copiunGUI\copiunGUI\Site1.Master.cs:210 System.Web.UI.Control.OnUnload(EventArgs e) )+8681754 System.Web.UI.Control.UnloadRecursive(Boolean dispose)+252 System.Web.UI.Control.UnloadRecursive(Boolean dispose)+188 System.Web.UI.Control.UnloadRecursive(Boolean dispose)+188 System. Web.UI.Control.UnloadRecursive(Boolean dispose)+188 System.Web.UI.Control.UnloadRecursive(Boolean dispose)+188 System.Web.UI.Page.UnloadRecursive(Boolean dispose)+23 System.Web.UI.Page .ProcessRequestCleanup()+43

web.config是:

<authentication mode="Forms">
    <forms loginUrl="Login.aspx" defaultUrl="~/Default.aspx" name="Cookie" timeout="10080" path="/">
    </forms>
</authentication>
<authorization>
    <deny users="?"/>
    <allow users="*"/>
</authorization>
Run Code Online (Sandbox Code Playgroud)

请帮忙.......

谢谢

谢谢你输入的人......

我试过这个:

if (Session["userName"] == null)
{
   Session.RemoveAll();
   Response.Redirect("~/Login.aspx?sessionError=" + "*Session Expired on pageload PleaseLog in again");
}
Run Code Online (Sandbox Code Playgroud)

但我得到错误:

在这种情况下无法获得响应.

Joh*_*nFx 9

问题出在这里:

if (Session["userName"].ToString() == null)
Run Code Online (Sandbox Code Playgroud)

当Session ["UserName"]是一个空对象引用时,你无法正确地调用.ToString().

试试这个......

if (Session["userName"] == null)...
Run Code Online (Sandbox Code Playgroud)